-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathEchoClient.java
More file actions
37 lines (28 loc) · 1.06 KB
/
EchoClient.java
File metadata and controls
37 lines (28 loc) · 1.06 KB
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
import java.net.Socket;
public class EchoClient {
public static void main(String[] args) throws Exception {
String screenName = args[0];
String host = args[1];
int port = 4444;
// connect to server and open up IO streams
Socket socket = new Socket(host, port);
In stdin = new In();
In in = new In(socket);
Out out = new Out(socket);
System.err.println("Connected to " + host + " on port " + port);
// read in a line from stdin, send to server, and print back reply
while (!stdin.hasNextLine()) {
// read line of client
String s = stdin.readLine();
// send over socket to server
out.println("[" + screenName + "]: " + s);
// get reply from server and print it out
StdOut.println(in.readLine());
}
// close IO streams, then socket
System.err.println("Closing connection to " + host);
out.close();
in.close();
socket.close();
}
}