【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() ;


目录
相关文章
|
安全 算法 网络安全
网络安全与信息安全:构建数字世界的坚固防线在数字化浪潮席卷全球的今天,网络安全与信息安全已成为维系社会秩序、保障个人隐私和企业机密的关键防线。本文旨在深入探讨网络安全漏洞的本质、加密技术的前沿进展以及提升公众安全意识的重要性,通过一系列生动的案例和实用的建议,为读者揭示如何在日益复杂的网络环境中保护自己的数字资产。
本文聚焦于网络安全与信息安全领域的核心议题,包括网络安全漏洞的识别与防御、加密技术的应用与发展,以及公众安全意识的培养策略。通过分析近年来典型的网络安全事件,文章揭示了漏洞产生的深层原因,阐述了加密技术如何作为守护数据安全的利器,并强调了提高全社会网络安全素养的紧迫性。旨在为读者提供一套全面而实用的网络安全知识体系,助力构建更加安全的数字生活环境。
|
消息中间件 缓存 网络协议
Netty基础—4.NIO的使用简介
本文详细介绍了Java NIO(New Input/Output)的核心概念与编程模型。首先,讲解了Buffer缓冲区的作用及4个核心概念:capacity、limit、position、mark,并通过Direct模式创建的Buffer示例展示了其高性能特点。接着,分析了Channel通道的概念,说明其与Buffer的关系以及FileChannel在文件读写中的应用,包括顺序写、随机写和多线程安全特性。 随后,对比了BIO(Blocking IO)编程模型的局限性,如线程资源耗尽问题,引出伪异步IO编程的改进方案,但指出其仍存在级联故障风险。进一步探讨了长连接与短连接的区别及其实现代码。
|
人工智能 运维 监控
阿里云携手神州灵云打造云内网络性能监测标杆 斩获中国信通院高质量数字化转型十大案例——金保信“云内网络可观测”方案树立云原生运维新范式
2025年,金保信社保卡有限公司联合阿里云与神州灵云申报的《云内网络性能可观测解决方案》入选高质量数字化转型典型案例。该方案基于阿里云飞天企业版,融合云原生引流技术和流量“染色”专利,解决云内运维难题,实现主动预警和精准观测,将故障排查时间从数小时缩短至15分钟,助力企业降本增效,形成可跨行业复制的数字化转型方法论。
722 6
|
Shell 网络架构 计算机视觉
YOLOv11改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
YOLOv11改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
707 14
|
Shell 网络架构 计算机视觉
RT-DETR改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
RT-DETR改进策略【模型轻量化】| ShufflenetV2,通过通道划分构建高效网络
336 5
|
存储 缓存 监控
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
1458 8
|
机器学习/深度学习 PyTorch 算法框架/工具
深度学习入门案例:运用神经网络实现价格分类
深度学习入门案例:运用神经网络实现价格分类
|
机器学习/深度学习 存储 自然语言处理
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)
1087 1
Netty BIO/NIO/AIO介绍
Netty BIO/NIO/AIO介绍
325 1
|
弹性计算 安全 容灾
阿里云DTS踩坑经验分享系列|使用VPC数据通道解决网络冲突问题
阿里云DTS作为数据世界高速传输通道的建造者,每周为您分享一个避坑技巧,助力数据之旅更加快捷、便利、安全。本文介绍如何使用VPC数据通道解决网络冲突问题。
695 0