Netty学习之---Selector

在入门学习netty的时候,我们就写过一个简单client连接service的代码;
1:client代码

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

2.service代码

ByteBuffer buffer = ByteBuffer.allocate(16);
        // 使用nio 来理解阻塞模式
        // 1.创建了服务器
        ServerSocketChannel ssc = ServerSocketChannel.open();

        // 默认是阻塞,可以设置为非阻塞
        ssc.configureBlocking(false);

        // 2.板顶监听端口
        ssc.bind(new InetSocketAddress(8080));

        // 3.连接集合
        List<SocketChannel>  channels = new ArrayList<>();
        while (true){
            // 4.accept 建立与客户端连接,socketChannel 用来与客户端直接通信
            SocketChannel sc =  ssc.accept();
            channels.add(sc);
            log.info("connected...");
            for(SocketChannel channel : channels){
                // 5.接收客户端发送的数据
                log.info("before read....",channel);
                channel.read(buffer);
                buffer.flip();
                buffer.clear();
                log.info("after read....",channel);
            }

        }

    }

以上即是一个简单的服务器与client的连接处理代码,这样代码也会有一个问题,就是如果client没有请求的时候,服务器端会一直连接着,造成资源的浪费。

3.使用Selector, 可以管理多个channel

 // 1.创建selector ,管理多个channel
        Selector selector = Selector.open();

        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        // 2.建立selector 和 channel的联系(注册)
        // SelectionKey 就是事件发生后,通过它可以得到该事件是什么事件和哪个channel事件
        SelectionKey sscKey = ssc.register(selector, 0, null);
        // 指明key 只关注 请求连接的事件
        sscKey.interestOps(SelectionKey.OP_ACCEPT);

        ssc.bind(new InetSocketAddress(8080));

        while (true) {
            // 3.select 方法
            // 没有事件就阻塞,有事件就恢复运行
            selector.select();

            // 4处理事件, 内部包含了所有的发生 事件
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {// 建立连接
                SelectionKey key = iterator.next();
                iterator.remove();
                // 5.区分事件类型
                if(key.isAcceptable()){
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                    SocketChannel sc = channel.accept();
                    // 读数据
                    sc.configureBlocking(false);
                    SelectionKey scKey = sc.register(selector, 0, null);
                    scKey.interestOps(SelectionKey.OP_READ);
                }else if(key.isReadable()){ // 读数据处理
                   // 拿到事件的出发channel
                    try{
                        SocketChannel channel = (SocketChannel) key.channel();
                        ByteBuffer buffer = ByteBuffer.allocate(16);
                        int read = channel.read(buffer);
                        if(read == -1){
                            //正常断开
                            key.cancel();
                        }
                        buffer.flip();
                    }catch (Exception e){
                        // 因为客户端断开连接, 解除连接
                       key.cancel();
                    }

                }
            }

        }


    }

在这种方式中, 由Selector管理channel。 如果client没有新的连接,管理的channel就会进入阻塞状态,直到下一个新的连接出来。
同时,由于Selector可以注册管理多个channel,可以根据类型进行处理不同的事件。

事件类型:
accept: 服务端有新的连接时会触发
connect: 客户端建立连接后触发
read:可读事件
write:可写事件

以及客户端正常和异常端口连接后,服务器端需要及时解除连接

 key.cancel();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

virtuousOne

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值