forked from yaojingguo/geekbang-zk-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoClient.java
34 lines (30 loc) · 1001 Bytes
/
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
package org.yao.socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* <pre>
* java -cp build/classes/java/main org.yao.socket.EchoClient 127.0.0.1 6000
* </pre>
*/
public class EchoClient {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java EchoClient <ip> <port number>");
System.exit(1);
}
String ip = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(ip, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
String msg;
while ((msg = stdin.readLine()) != null) {
out.println(msg);
System.out.println("echo: " + in.readLine());
}
}
}
}