11.NIO核心三:选择器(Selector)

 

.

4.接收:SelectionKey.OP_ACCEPT;

NIO非阻塞式网络通信的原理分析

服务端样例:
    /**
     * Nio非阻塞通信下的入门案例:服务端开发
     */
    public class Server {
        public static void main(String[] args) throws Exception {
            System.out.println("-----------------服务端启动-------------");
            //1.获取通道
            ServerSocketChannel ssChannel = ServerSocketChannel.open();
            //2.切换为非阻塞模式
            ssChannel.configureBlocking(false);
            //3.绑定连接的端口
            ssChannel.bind(new InetSocketAddress(9999));
            //4.获取选择器
            Selector selector = Selector.open();
            //5.将通道注册到选择器上去,并且开始指定监听事件
            ssChannel.register(selector, SelectionKey.OP_ACCEPT);
            //6.轮询选择器上就绪好的事件
            while (selector.select() > 0) {
                //7.获取选择器中所有注册的通道中已经就绪好的事件
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                //8.开始遍历这些准备好的事件
                while (iterator.hasNext()) {
                    SelectionKey sk = iterator.next();
                    //9.判断这些事件具体是什么
                    if (sk.isAcceptable()) {
                        //10.获取当前接入的客户端通道
                        SocketChannel schannel = ssChannel.accept();
                        //11.切换成成非阻塞模式
                        schannel.configureBlocking(false);
                        //12.将客户端通道注册到选择器上
                        schannel.register(selector, SelectionKey.OP_READ);
                    } else if (sk.isReadable()) {
                        StringBuffer sb = new StringBuffer();
                        //13.获取当前选择器的读就绪事件
                        SocketChannel schannel = (SocketChannel) sk.channel();
                        //14.读取数据
                        ByteBuffer buf = ByteBuffer.allocate(1024);
                        int len = 0;
                        while ((len = schannel.read(buf)) > 0) {
                            System.out.println("len:"+len);
                            buf.flip();
                            sb.append(new String(buf.array(), 0, len));
                            buf.clear();
                        }
                        System.out.println("客户端报文:"+sb.toString());
                    }
                    iterator.remove();
                }
            }
        }
    }
    
客户端样例:
    public class Client {
        public static void main(String[] args) throws IOException {
            //1.获取通道
            SocketChannel schannel=SocketChannel.open(new InetSocketAddress("127.0.0.1",9999));
            //2.切换成非阻塞模式
            schannel.configureBlocking(false);
            //3.指定缓存区大小
            ByteBuffer byteBuffer=ByteBuffer.allocateDirect(1024);
            //4.发送数据给服务端
            Scanner sc=new Scanner(System.in);
            while (true){
                System.out.println("请说...");
                String msg=sc.nextLine();
                byteBuffer.put(("吴孟达:"+msg).getBytes());
                byteBuffer.flip();
                schannel.write(byteBuffer);
                byteBuffer.clear();
            }
        }
    }
    

可以正常接收!并且可以同时几个客户端连接!

 

posted @ 2022-08-19 08:27  努力的达子  阅读(65)  评论(0编辑  收藏  举报