Skip to content

Commit 0589b95

Browse files
authored
Java Threads
1 parent 72771e7 commit 0589b95

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

Threads/GreetingClient.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package myjavanet;
2+
3+
/**
4+
*
5+
* @author Nikos Konstantakis
6+
*/
7+
8+
import java.net.*;
9+
import java.io.*;
10+
11+
public class GreetingClient {
12+
13+
// File Name GreetingClient.java
14+
15+
public static void main(String [] args) {
16+
String serverName = args[0];
17+
int port = Integer.parseInt(args[1]);
18+
try {
19+
System.out.println("Connecting to " + serverName + " on port " + port);
20+
Socket client = new Socket(serverName, port);
21+
22+
System.out.println("Just connected to " + client.getRemoteSocketAddress());
23+
OutputStream outToServer = client.getOutputStream();
24+
DataOutputStream out = new DataOutputStream(outToServer);
25+
26+
out.writeUTF("Hello from " + client.getLocalSocketAddress());
27+
InputStream inFromServer = client.getInputStream();
28+
DataInputStream in = new DataInputStream(inFromServer);
29+
30+
System.out.println("Server says " + in.readUTF());
31+
client.close();
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}

Threads/GreetingServer.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package myjavanet;
2+
3+
/**
4+
*
5+
* @author Nikos Konstantakis
6+
*/
7+
8+
import java.net.*;
9+
import java.io.*;
10+
11+
public class GreetingServer extends Thread {
12+
13+
// File Name GreetingServer.java
14+
15+
private ServerSocket serverSocket;
16+
17+
public GreetingServer(int port) throws IOException {
18+
serverSocket = new ServerSocket(port);
19+
serverSocket.setSoTimeout(60000);
20+
}
21+
22+
public void run() {
23+
while(true) {
24+
try {
25+
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
26+
Socket server = serverSocket.accept();
27+
28+
System.out.println("Just connected to " + server.getRemoteSocketAddress());
29+
DataInputStream in = new DataInputStream(server.getInputStream());
30+
31+
System.out.println(in.readUTF());
32+
DataOutputStream out = new DataOutputStream(server.getOutputStream());
33+
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
34+
+ "\nGoodbye!");
35+
server.close();
36+
37+
} catch (SocketTimeoutException s) {
38+
System.out.println("Socket timed out!");
39+
break;
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
break;
43+
}
44+
}
45+
}
46+
47+
public static void main(String [] args) {
48+
int port = Integer.parseInt(args[1]);
49+
System.out.println(args[0]);
50+
try {
51+
Thread t = new GreetingServer(port); // t is a thread
52+
t.start(); // calls the run function
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
}

Threads/MyJavaNet.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package myjavanet;
2+
3+
/**
4+
*
5+
* @author Nikos Konstantakis
6+
*/
7+
8+
public class MyJavaNet {
9+
/**
10+
* @param args the command line arguments
11+
*/
12+
public static void main(String[] args) {
13+
// TODO code application logic here
14+
if(args.length != 0) {
15+
System.out.println("Arg 0:"+args[0]);
16+
System.out.println("Arg 1:"+args[1]);
17+
}
18+
else {
19+
System.out.println("No arguments");
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)