-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoClient.java
47 lines (40 loc) · 1.15 KB
/
DemoClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Author: Luigi Vincent
* ACM Networking workshop demo client
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class DemoClient {
private final static String ADDRESS = "localhost";
private final static int PORT = 9001;
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
Socket socket = new Socket(ADDRESS, PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String serverMessage = null;
if ((serverMessage = in.readLine()) != null) {
System.out.println(serverMessage);
System.out.print("Enter your name: ");
out.println(keyboard.nextLine());
}
new Thread(() -> {
String input = null;
while (true) {
try {
if ((input = in.readLine()) != null) {
System.out.println(input);
}
} catch (IOException ioe) {
// no time for this
}
}
}).start();
while (true) {
out.println(keyboard.nextLine());
}
}
}