forked from yaojingguo/geekbang-zk-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoServer.java
65 lines (59 loc) · 1.63 KB
/
EchoServer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.yao.socket;
import com.google.common.io.Closeables;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* <pre>
* java -cp build/classes/java/main org.yao.socket.EchoServer 6000
* </pre>
*/
public class EchoServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java EchoServer <port number>");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
ServerSocket serverSocket = null;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
// Method 1
// serverSocket = new ServerSocket(Integer.parseInt(args[0]));
// Method 2
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
InetSocketAddress address = new InetSocketAddress(port);
serverSocket.bind(address);
socket = serverSocket.accept();
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg;
while ((msg = in.readLine()) != null) {
out.println(msg);
}
} finally {
close(out);
close(in);
close(socket);
close(serverSocket);
}
}
private static void close(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (Throwable th) {
throw new RuntimeException(th);
}
}
}