-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleReactorServer.java
68 lines (60 loc) · 3.47 KB
/
SingleReactorServer.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
66
67
68
package io.nio.singlereactor;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class SingleReactorServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress("127.0.0.1", 8899));
// 配置了非阻塞才能往selector注册
ssc.configureBlocking(false);
// 专注于客户端连接事件 --> 客户端的连接操作,对于服务端而言就是接收事件
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
// 线程为中断则一直执行
while (!Thread.interrupted()) {
System.out.println("等待客户端连接..");
// 此方法是阻塞的,当selector有事件发生时,才会解除阻塞
selector.select();
// 获取注册在selector中的channel的感兴趣事件 --> 只有是感兴趣的事件才会被放到set中
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
if (selectionKey.isAcceptable()) {
System.out.println("有客户端连接了..,获取客户端的socketChannel并注册到selector中,同时绑定客户端的读事件");
// 先获取到服务端的channel,然后再获取连接到服务端的客户端channel
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
SocketChannel clientSocketChannel = serverSocketChannel.accept();
clientSocketChannel.configureBlocking(false);
clientSocketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
System.out.println("有客户端向服务器发送数据了..");
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
SocketChannel clientSocketChannel = (SocketChannel) selectionKey.channel();
StringBuilder sb = new StringBuilder();
while (clientSocketChannel.read(byteBuffer) > 0) {
// 切换成读模式,读取byteBuffer的数据
byteBuffer.flip();
sb.append(new String(byteBuffer.array(), 0, (byteBuffer.limit() - byteBuffer.position())));
}
System.out.println("客户端发来的数据:" + sb);
// 往客户端回写数据
ByteBuffer writeBuffer = ByteBuffer.wrap("hello client!".getBytes());
clientSocketChannel.write(writeBuffer);
selectionKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} else if (selectionKey.isWritable()) {
System.out.println("处理服务端的写事件,无数据可写时,将感兴趣的写事件去除");
selectionKey.interestOps(SelectionKey.OP_READ);
}
iterator.remove();
}
}
}
}