栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Netty学习之---Selector

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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  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 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();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/941192.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号