【Netty】NIO 选择器 ( Selector ) 通道 ( Channel ) 缓冲区 ( Buffer ) 网络通信案例

简介: 【Netty】NIO 选择器 ( Selector ) 通道 ( Channel ) 缓冲区 ( Buffer ) 网络通信案例

文章目录

I . SelectionKey 简介

II . SelectionKey 事件简介

III . SelectionKey 常用 API 简介





I . SelectionKey 简介


1 . 通道注册给选择器 : 通道 ( Channel ) 注册给 选择器 ( Selector ) , 该通道就会纳入到该 选择器 ( Selector ) 管理范畴 , 选择器 ( Selector ) 可以监听通道的事件 ;


被注册的通道说明 : 这个 通道 ( Channel ) 即可以是 服务器套接字通道 ( ServerSocketChannel ) , 也可以是 套接字通道 ( SocketChannel ) ;



2 . 选择器真实类型 : 选择器 ( Selector ) 的 Selector 类是抽象类 , 其实例化的 真实类型是 WindowsSelectorImpl ;



3 . 选择器 ( Selector ) 管理 通道 ( Channel ) 的方式 : 当 通道 ( Channel ) 注册给 选择器 ( Selector ) 后 , 会返回 SelectionKey , 并将该 SelectionKey 放入 选择器 ( Selector ) 中的 HashSet<SelectionKey> keys 集合中 , 这个集合中存放了所有注册给该 选择器 ( Selector ) 的通道所代表的 SelectionKey ;



4 . 获取有事件发生的通道对应的 SelectionKey 集合 : 当 选择器 ( Selector ) 监听到有事件发生 , 此时只能监听到事件发生的个数 , 不知道具体的情况 ; 这就需要自己去 调用 选择器 ( Selector ) 的 selectedKeys() 方法 , 此时返回的是 Set<SelectionKey> 类型的集合 , 因为同事可能有多个通道有事件发生 , 这里可以一次性处理多个通道的事件 ;






II . SelectionKey 事件简介


SelectionKey 中的事件 , 就是 选择器 ( Selector ) 注册通道时 , 需要指明 , 监听这个通道的哪些事件 ;



SelectionKey 中定义了四种事件 : 数据读取 ( OP_READ ) , 数据写出 ( OP_WRITE ) , 连接 ( OP_CONNECT ) , 接受连接 ( OP_ACCEPT ) ;




1 . 接受连接 ( OP_ACCEPT ) 事件 :



① 适用场景 : 服务器端 服务器套接字通道 ( ServerSocketChannel ) 注册该事件给 选择器 ( Selector ) , 选择器 ( Selector ) 可以监听到客户端的连接请求 ;


② 代码示例 : 下面的代码作用是 , 将 ServerSocketChannel 通道的 SelectionKey.OP_ACCEPT 事件注册给 选择器 ( Selector ) , 当有客户端连接服务器的时候 , 就会触发 选择器 的监听方法 ;


//将 serverSocketChannel 通道注册给 选择器 ( Selector ), 这里注册连接事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);


③ OP_ACCEPT 常量原型 : 该常量定义在 SelectionKey.java 类中 , 该值的大小是 1 << 4 , 0b10000 ( 二进制 ) , 0x10 ( 十六进制 ) , 16 ( 十进制 ) ;

/**
 * Operation-set bit for socket-accept operations.
 *
 * <p> Suppose that a selection key's interest set contains
 * <tt>OP_ACCEPT</tt> at the start of a <a
 * href="Selector.html#selop">selection operation</a>.  If the selector
 * detects that the corresponding server-socket channel is ready to accept
 * another connection, or has an error pending, then it will add
 * <tt>OP_ACCEPT</tt> to the key's ready set and add the key to its
 * selected-key&nbsp;set.  </p>
 */
public static final int OP_ACCEPT = 1 << 4;




2 . 读取数据 ( OP_READ ) 事件 :



① 适用场景 : 服务器端 套接字通道 ( SocketChannel ) 注册该事件给 选择器 ( Selector ) , 选择器 ( Selector ) 可以监听到客户端的 数据写入到服务器 , 也就是说 服务器端需要执行 读取数据 的工作 ;


② 代码示例 : 下面的代码作用是 , 将 SocketChannel 通道的 SelectionKey.OP_READ 事件注册给 选择器 ( Selector ) , 当有客户端上传数据的时候 , 就会触发 选择器 的监听方法 ;


//注册通道 : 将 SocketChannel 通道注册给 选择器 ( Selector )
//关注事件 : 关注事件时读取事件, 服务器端从该通道读取数据
//关联缓冲区 :
sc.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));


③ OP_READ 常量原型 : 该常量定义在 SelectionKey.java 类中 , 该值的大小是 1 << 0 , 0b1 ( 二进制 ) , 0x1 ( 十六进制 ) , 1 ( 十进制 ) ;


/**
 * Operation-set bit for read operations.
 *
 * <p> Suppose that a selection key's interest set contains
 * <tt>OP_READ</tt> at the start of a <a
 * href="Selector.html#selop">selection operation</a>.  If the selector
 * detects that the corresponding channel is ready for reading, has reached
 * end-of-stream, has been remotely shut down for further reading, or has
 * an error pending, then it will add <tt>OP_READ</tt> to the key's
 * ready-operation set and add the key to its selected-key&nbsp;set.  </p>
 */
public static final int OP_READ = 1 << 0;



3 . 写出数据 ( OP_WRITE ) 事件 :



① 适用场景 : 将通道 ( Channel ) 注册给 选择器 ( Selector ) , 注册该 写出数据 ( OP_WRITE ) 事件 , 如果选择器触发该事件 , 表示该 向该 通道 ( Channel ) 写出数据了 ;


② OP_WRITE 常量原型 : 该常量定义在 SelectionKey.java 类中 , 该值的大小是 1 << 2 , 0b100 ( 二进制 ) , 0x4 ( 十六进制 ) , 4 ( 十进制 ) ;


/**
 * Operation-set bit for write operations.
 *
 * <p> Suppose that a selection key's interest set contains
 * <tt>OP_WRITE</tt> at the start of a <a
 * href="Selector.html#selop">selection operation</a>.  If the selector
 * detects that the corresponding channel is ready for writing, has been
 * remotely shut down for further writing, or has an error pending, then it
 * will add <tt>OP_WRITE</tt> to the key's ready set and add the key to its
 * selected-key&nbsp;set.  </p>
 */
public static final int OP_WRITE = 1 << 2;


4 . 连接 ( OP_CONNECT ) 事件 :



① 适用场景 : 将通道 ( Channel ) 注册给 选择器 ( Selector ) , 注册该 连接 ( OP_CONNECT ) 事件 , 如果选择器触发该事件 , 表示该 发起网络 Socket 连接了 ;


② OP_WRITE 常量原型 : 该常量定义在 SelectionKey.java 类中 , 该值的大小是 1 << 3 , 0b1000 ( 二进制 ) , 0x8 ( 十六进制 ) , 8 ( 十进制 ) ;


/**
 * Operation-set bit for socket-connect operations.
 *
 * <p> Suppose that a selection key's interest set contains
 * <tt>OP_CONNECT</tt> at the start of a <a
 * href="Selector.html#selop">selection operation</a>.  If the selector
 * detects that the corresponding socket channel is ready to complete its
 * connection sequence, or has an error pending, then it will add
 * <tt>OP_CONNECT</tt> to the key's ready set and add the key to its
 * selected-key&nbsp;set.  </p>
 */
public static final int OP_CONNECT = 1 << 3;






III . SelectionKey 常用 API 简介


1 . 获取 NIO 三大组件 选择器 ( Selector ) , 通道 ( Channel ) , 缓冲区 ( Buffer ) 方法 :



① 获取 选择器 ( Selector ) : Selector selector() , 调用该方法获取对应的 选择器 ( Selector ) ;


② 获取 通道 ( Channel ) : SelectableChannel channel() , 调用该方法 获取对应绑定的 通道 ( Channel ) ;


③ 获取 缓冲区 ( Buffer ) : Object attach(Object ob) , 调用该方法 获取 注册的 通道 ( Channel ) 对应的 缓冲区 ( Buffer ) ;


④ 代码示例 : 这是上一篇博客中 NIO 通信案例中的 服务器端的部分代码 , 涉及到了获取 通道 和 缓冲区 操作 ;


//获取 通道 ( Channel ) : 通过 SelectionKey 获取
SocketChannel socketChannel = (SocketChannel) key.channel();
//获取 缓冲区 ( Buffer ) : 获取到 通道 ( Channel ) 关联的 缓冲区 ( Buffer )
ByteBuffer byteBuffer = (ByteBuffer) key.attachment();



2 . 事件相关的方法 :



① 设置或更改监听事件 : interestOps(int ops) , 设置 或 改变 选择器 ( Selector ) 关联的事件 ;


② 判定事件类型 :


是否是 OP_READ 事件 : boolean isReadable() ;

是否是 OP_WRITE 事件 : boolean isWritable() ;

是否是 OP_CONNECT 事件 : boolean isConnectable() ;

是否是 OP_ACCEPT 事件 : boolean isAcceptable() ;


相关文章
网络安全与信息安全:构建数字世界的坚固防线在数字化浪潮席卷全球的今天,网络安全与信息安全已成为维系社会秩序、保障个人隐私和企业机密的关键防线。本文旨在深入探讨网络安全漏洞的本质、加密技术的前沿进展以及提升公众安全意识的重要性,通过一系列生动的案例和实用的建议,为读者揭示如何在日益复杂的网络环境中保护自己的数字资产。
本文聚焦于网络安全与信息安全领域的核心议题,包括网络安全漏洞的识别与防御、加密技术的应用与发展,以及公众安全意识的培养策略。通过分析近年来典型的网络安全事件,文章揭示了漏洞产生的深层原因,阐述了加密技术如何作为守护数据安全的利器,并强调了提高全社会网络安全素养的紧迫性。旨在为读者提供一套全面而实用的网络安全知识体系,助力构建更加安全的数字生活环境。
YOLOv11改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
YOLOv11改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
112 14
RT-DETR改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
RT-DETR改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
49 5
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
376 7
阿里云DTS踩坑经验分享系列|使用VPC数据通道解决网络冲突问题
阿里云DTS作为数据世界高速传输通道的建造者,每周为您分享一个避坑技巧,助力数据之旅更加快捷、便利、安全。本文介绍如何使用VPC数据通道解决网络冲突问题。
228 0
【优秀python毕设案例】基于python django的新媒体网络舆情数据爬取与分析
本文介绍了一个基于Python Django框架开发的新媒体网络舆情数据爬取与分析系统,该系统利用Scrapy框架抓取微博热搜数据,通过SnowNLP进行情感分析,jieba库进行中文分词处理,并以图表和词云图等形式进行数据可视化展示,以实现对微博热点话题的舆情监控和分析。
417 3
【优秀python毕设案例】基于python django的新媒体网络舆情数据爬取与分析
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)
一文讲明TCP网络编程、Socket套接字的讲解使用、网络编程案例
这篇文章全面讲解了基于Socket的TCP网络编程,包括Socket基本概念、TCP编程步骤、客户端和服务端的通信过程,并通过具体代码示例展示了客户端与服务端之间的数据通信。同时,还提供了多个案例分析,如客户端发送信息给服务端、客户端发送文件给服务端以及服务端保存文件并返回确认信息给客户端的场景。
一文讲明TCP网络编程、Socket套接字的讲解使用、网络编程案例
【网络工程师配置篇】BGP联盟配置案例及分析(超级干货)
【网络工程师配置篇】BGP联盟配置案例及分析(超级干货)
371 2
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等