-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoClient.java
58 lines (32 loc) · 1.18 KB
/
EchoClient.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
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public final class EchoClient {
public static void main(String[] args) throws Exception {
try (Socket socket = new Socket("localhost", 22222)) {
//User Input
In userInput = new In();
//Input from server
In in = new In(socket);
//Output to server
OutputStream os = socket.getOutputStream();
PrintStream out = new PrintStream(os, true, "UTF-8");
//First read through of server
System.out.println(in.readLine());
System.out.println(in.readLine());
//While no user input
while(true) {
//read from client
String s = userInput.readLine();
if(s.equalsIgnoreCase("exit")) break;
//Send over to socket
out.println(s);
//get reply from server and print it out
System.out.println("Server: " + in.readLine());
}
}catch (IOException e) {
System.out.println(e);
}
}
}