NIO AIO

#NIO的服务端开发流程:
在这里插入图片描述
在这里插入图片描述

NIO的客户端编程

在这里插入图片描述
在这里插入图片描述

 @Test
    public void servi(){
        try {
            ServerSocketChannel serverSocketChannel  = ServerSocketChannel.open ();
            serverSocketChannel.bind (new InetSocketAddress (8893));
            System.out.println ("绑定成功");
            serverSocketChannel.configureBlocking (false);
            Selector selector = Selector.open ();
            serverSocketChannel.register (selector , SelectionKey.OP_ACCEPT);
            while (selector.select ()>0) {
                Iterator<SelectionKey> iterator = selector.selectedKeys ().iterator ();
                while (iterator.hasNext ()) {
                    SelectionKey selectionKey = iterator.next ();
                    iterator.remove ();
                    if (selectionKey.isAcceptable ()) {
                        System.out.println ("有事件要发生 ");
                        ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel) selectionKey.channel ();
                        SocketChannel socketChannel = serverSocketChannel1.accept ();
                        socketChannel.configureBlocking (false);
                        socketChannel.register (selector , SelectionKey.OP_READ);
                    }
                    if (selectionKey.isReadable ()) {
                        System.out.println ("有读事件发生");
                        SocketChannel channel = (SocketChannel) selectionKey.channel ();
                        ByteBuffer allocate = ByteBuffer.allocate (1024);
                        int read = channel.read (allocate);
                        if (read == -1){
                            channel.close ();
                        }
                        allocate.flip ();
                        byte[] bytes = new byte[allocate.remaining ()];
                        allocate.get (bytes);
                        String s = new String (bytes);
                        System.out.println (s);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
    @Test
    public void cli() {
        try {
            SocketChannel socketChannel = SocketChannel.open ();
            socketChannel.configureBlocking (false);
            Selector selector = Selector.open ();
            if (!socketChannel.connect (new InetSocketAddress ("127.0.0.1" , 8893))){
                socketChannel.register (selector , SelectionKey.OP_CONNECT);
                selector.select ();
                Iterator<SelectionKey> iterator = selector.selectedKeys ().iterator ();
                while (iterator.hasNext ()) {
                    SelectionKey selectionKey = iterator.next ();
                    iterator.remove ();
                    if (selectionKey.isConnectable ()){
                        SocketChannel socketChannel1 = (SocketChannel) selectionKey.channel ();
                        socketChannel1.finishConnect ();
                    }
                }
            }
            System.out.println ("连接成功");
            ByteBuffer buffer = ByteBuffer.allocate (1024);
            buffer.put ("hello".getBytes ());
            buffer.flip ();
            socketChannel.write (buffer);
            socketChannel.close ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值