NIO学习总结小dome

面向过程编程

服务端

/**
 * 功能描述: 服务端
 *
 * @author Songxianyang
 * @date 2022-09-03 21:53
 */
public class Server {
    @SneakyThrows
    public static void main(String[] args) {
        // 获取serverSocketChannel
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        
        // 获取 selector
        Selector selector = Selector.open();
        // 设置非阻塞
        serverSocketChannel.configureBlocking(false);
        
        // 设置端口
        serverSocketChannel.bind(new InetSocketAddress(9999));
        // 把serverSocketChannel 注册到selector
        // OP_ACCEPT 客户端连接
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        // 不停的监听事件
        while (true) {
            //获取选择器中的 连接事件(如果1s没有连接就打印下面语句)
            if (selector.select(1000) <= 0) {
                System.out.println("如果1s没有连接就打印下面语句");
            }
            // 获取选择器中的真是事件
            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> keysIterator = keys.iterator();
            // 去找到具体的事件
            while (keysIterator.hasNext()) {
                SelectionKey key = keysIterator.next();
                // 获取客户端连接事件
                if (key.isAcceptable()) {
                    // 获取客户端 socketChannel
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    // 客户端socketChannel 设置非阻塞
                    socketChannel.configureBlocking(false);
                    // 客户端socketChannel 注册到 select上(从通道里面读取数据)
                    socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                }
                // 判断我要从key中读到具体的事件(读)
                if (key.isReadable()) {
                    // 获取SelectionKey中的读Channel
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    // 获取byteBuffer
                    ByteBuffer byteBuffer =(ByteBuffer) key.attachment();
                    socketChannel.read(byteBuffer);
                    System.out.println(new String(byteBuffer.array()));
                }
                keysIterator.remove();
            }
        }
        
    }
}

客户端

/**
 * 功能描述: 客户端
 *
 * @author Songxianyang
 * @date 2022-09-03 22:40
 */
public class C {
    @SneakyThrows
    public static void main(String[] args) {
        SocketChannel socketChannel = SocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9999);
        socketChannel.connect(inetSocketAddress);
        socketChannel.configureBlocking(false);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入:");
            String msg = scanner.nextLine();
            // 往buffer 放数据
            buffer.put(msg.getBytes());
            buffer.flip();
            // socketChannel 放buffer
            socketChannel.write(buffer);
            buffer.clear();
        }
    }
}

给大家预留一个bug

运行即可!

推荐的学习视频

尚硅谷

小小的总结一下

nio非阻塞,表现两个方便。1、首先要开启非阻塞 2、获取到事件后再与客户端Channel建立连接

代码

 // 1设置非阻塞
        serverSocketChannel.configureBlocking(false);
// 2判断我要从key中读到具体的事件(读)
                if (key.isReadable()) {
                    // 获取SelectionKey中的读Channel
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    }        

单词错了 也方便大家理解

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值