NIO非阻塞模式

在这里插入图片描述

代码

server

package com.example;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

import static com.example.util.ByteBufferUtil.debugRead;

@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        // 使用nio来理解 【阻塞模式】

        // 0.ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);

        // 1.Opens a server-socket channel
        ServerSocketChannel ssc = ServerSocketChannel.open();
        // 非阻塞模式
        ssc.configureBlocking(false);

        // 2.绑定监听端口
        ssc.bind(new InetSocketAddress(8080));

        // 3.连接集合
        List<SocketChannel> channels = new ArrayList<>();
        while(true){
            // 4.accept 建立与客户端的连接 socketChannel 用来和客户端之间通信
            // 非阻塞模式下,如果没有建立连接,返回null
            SocketChannel sc = ssc.accept();
            if(sc != null){
                log.debug("connected...{}",sc);
                sc.configureBlocking(false);
                channels.add(sc);
            }
            for(SocketChannel channel : channels){
                // 5.接受客户端发送的数据
                int read = channel.read(buffer);
                if(read > 0){
                    buffer.flip();
                    debugRead(buffer);
                    buffer.clear();
                    log.debug("after read ...{}", channel);
                }
            }
        }
    }
}

client

package com.example;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class Client {
    public static void main(String[] args) throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost",8080));
        System.out.println("waiting...");
    }
}

说明

  • ssc.configureBlocking(false); 将server-scoket-channel设置为非阻塞模式,运行ssc.accept()时,如果没有获取到连接,就返回null,不会发生阻塞。

  • sc.configureBlocking(false);将client-socket-channel设置为非阻塞模式,运行channel.read(buffer)时,将channel的数据读取进buffer,如果没有从channel中没有读取到数据,会返回0,不会发生阻塞。

调试

启动server

while(true)无限循环,没有任何输出。

启动第一个client

第一个client端口为55537

21:21:18.999 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55537]

启动第二、三个client

第二个client端口为55545,第三个client端口为55551

21:21:18.999 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55537]
21:21:23.113 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55545]
21:21:27.077 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55551]

第二个客户端发送数据

执行sc.write(Charset.defaultCharset().encode("hello~!")),服务端收到消息后输出。

21:21:18.999 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55537]
21:21:23.113 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55545]
21:21:27.077 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55551]
22:41:44.651 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [7]
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 7e 21                            |hello~!         |
+--------+-------------------------------------------------+----------------+
22:41:44.668 [main] DEBUG com.example.Server - after read ...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:55545]

缺点

单线程非阻塞模式的缺点

没有连接和channel数据可读时,会一直进行循环,浪费cpu资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值