在传统的同步阻塞模型开发中,ServerSocket绑定监听端口,Socket发起连接操作。连接成功后,双方通过输入输出流进行同步阻塞式通信。

下面以时间服务器为例说明。
客户端向服务端发送“Server Time”,服务端向客户端返回服务端当前时间。

服务端代码:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @author j.tommy
* Created by j.tommy on 2017/11/11.
*/
public class TimeServer {
public static void main(String[] args) {
TimeServer timeServer = new TimeServer();
timeServer.start(8080);
}
private void start(int port) {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println("TimeServer is running...");
while (true) {
Socket socket = ss.accept();
new Thread(new TimeServerHandler(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
es.shutdownNow();
}
}
}
class TimeServerHandler implements Runnable {
private Socket socket = null;
public TimeServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
while (true) {
String input = br.readLine();
if (null == input) {
break;
}
System.out.println("接收到客户端请求:" + input);
if ("Server Time".equalsIgnoreCase(input)) {
pw.println((new Date()).toString());
}
}
} catch (IOException e) {
System.out.println("ip:" + socket.getInetAddress().getHostAddress() + " I/O异常");
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (pw != null) {
pw.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}

客户端代码:

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
/**
* @author j.tommy
* Created by j.tommy on 2017/11/11.
*/
public class TimeClient {
public static void main(String[] args) {
TimeClient tc = new TimeClient();
tc.connect("127.0.0.1",8080);
}
private void connect(String host,int port) {
Socket socket = null;
InputStream in = null;
OutputStream out = null;
try {
socket = new Socket(host,port);
in = socket.getInputStream();
out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out),true);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
System.out.println("请求服务器时间");
pw.println("Server Time");
String response = br.readLine();
System.out.println("response:" + response);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

由一个Acceptor线程负责监听客户端连接请求,接收到客户端连接请求后,为每个客户端分配一个新的线程进行链路处理。处理完成后,通过输出流应答客户端。

BIO模型的最大问题在于缺乏弹性伸缩能力,因为服务端线程和客户端是呈现的1:1的关系。当线程数膨胀后,系统性能急剧下降,随着并发访问量的继续增大,系统会发生线程堆栈溢出、创建新线程失败等问题,最终导致进程宕机,不能对外提供服务。
参考《Netty权威指南》