Netty系列-NIO非阻塞网络编程原理分析

# Netty系列-NIO非阻塞网络编程原理分析

  1. 当客户端连接时会通过ServerSocketChannel得到SocketChannel。
  2. 将SocketChannel实例注册到Selector上register(Selector sel, int ops, Object att)
  3. 注册并返回SelectionKey,会和该Selector关联(集合)。
  4. Selector进行监听select()方法,返回有事件发生的通道个数。
  5. 进一步得到各个事件响应的SelectionKey
  6. 再通过SelectionKey的channel()方法获取所注册的SocketChannel

# 示例代码

@Test
public void ClientTest() throws IOException {
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 6666);
    if (!socketChannel.connect(address)) {
        while (!socketChannel.finishConnect()) 
          System.out.println("Client:连接需要时间,客户端不会阻塞");
    }
    String str = "Hello Server";
    ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
    socketChannel.write(buffer);
    int read = System.in.read();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
public void ServerTest() throws IOException {
    //获取ServerSocketChannel实例
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    //获取Selector实例
    Selector selector = Selector.open();
    //绑定IP地址和端口
    serverSocketChannel.socket().bind(new InetSocketAddress(6666));
    //配置为非阻塞模式
    serverSocketChannel.configureBlocking(false);
    //将ServerSocketChannel实例注册到Selector实例中,标识为OP_ACCEPT
    serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        //等待1000ms,无事件发生则返回
        if (selector.select(1000) == 0) {
            System.out.println("Server:等待一秒,无连接");
            continue;
        }
        //获取有事件发生的SelectionKey集合
        Set<SelectionKey> selectionKeys = selector.selectedKeys();
        //遍历集合
        Iterator<SelectionKey> iterator = selectionKeys.iterator();
        while (iterator.hasNext()) {
            SelectionKey selectionKey = iterator.next();
            //如果是OP_ACCEPT,表明有客户端连接
            if (selectionKey.isAcceptable()) {
                //为该客户端生成SocketChannel实例
                SocketChannel accept = serverSocketChannel.accept();
                accept.configureBlocking(false);
                //并将SocketChannel实例注册到Selector中,标识为OP_READ,并关联Buffer。
                accept.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
            }
            //如果是OP_READ,表明客户端已经将数据写入SocketChannel实例
            if (selectionKey.isReadable()) {
                //获取已经变化的SocketChannel实例
                SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                //获取到关联的Buffer
                ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
                //读取数据
                socketChannel.read(buffer);
                System.out.println("from Cline:"+new String(buffer.array()));
            }
            //移除已经处理的事件,防止重复处理。
            selectionKeys.remove(selectionKey);
        }
    }
}
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

# NIO与零拷贝

上次更新: 2020/07/25, 16:07:00
最近更新
01
RabbitMQ简介
10-27
02
聊聊Java多态
10-21
03
JVM垃圾回收器
10-16
更多文章>