NIO 源码分析(01) NIO 最简用法
NIO 源码分析(01) NIO 最简用法
Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html)
Java NIO 主要由三个部分组成:Channel、Buffer 和 Selector。在分析源码前最好对 NIO 的基本用法和 Linux NIO 在一个基本的了解。
本文会提供一个 NIO 最简使用示例,之后的源码分析都会基于该示例及其扩展进行。
一、服务端
public class Server implements Runnable {
public static void main(String[] args) {
new Thread(new Server(8765)).start();
}
// 1 多路复用器(管理所有的通道)
private Selector selector;
// 2 建立缓冲区
private ByteBuffer readBuf = ByteBuffer.allocate(1024);
private ByteBuffer writeBuf = ByteBuffer.allocate(1024);
public Server(int port) {
try {
//1. 获取多路复用器
this.selector = Selector.open();
//2. 获取服务器端通道
ServerSocketChannel ssChannel = ServerSocketChannel.open();
//3. 切换成非阻塞模式
ssChannel.configureBlocking(false);
//4. 绑定端口号
ssChannel.bind(new InetSocketAddress(port));
//5. 把服务器通道注册到多路复用器上,并且监听阻塞事件
ssChannel.register(this.selector, SelectionKey.OP_ACCEPT);
System.out.println("Server start, port :" + port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
// 1. 阻塞到至少有一个通道在你注册的事件上就绪,返回的 int 值表示有多少通道已经就绪
int wait = selector.select();
if (wait == 0) continue;
//2. 遍历多路复用器上已经准备就绪的通道
Iterator<SelectionKey> iterator = this.selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isValid()) {
if (key.isAcceptable()) {
// 3.1 如果为 accept 状态,就获取客户端通道并注册到selector上
this.accept(key);
} else if (key.isReadable()) {
// 3.2 如果为可读状态
this.read(key);
} else if (key.isWritable()) {
// 3.3 写数据
this.write(key);
}
}
// 4. 注意必须在处理完通道时自己移除
iterator.remove();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void write(SelectionKey key) {
//ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
//ssc.register(this.seletor, SelectionKey.OP_WRITE);
}
private void read(SelectionKey key) {
try {
//1. 清空缓冲区旧的数据
this.readBuf.clear();
//2. 获取之前注册的socket通道对象
SocketChannel sChannel = (SocketChannel) key.channel();
//3. 读数据
int len = sChannel.read(this.readBuf);
if (len == -1) {
key.channel().close();
key.cancel();
return;
}
//5 有数据则进行读取 读取之前要flip()复位
this.readBuf.flip();
byte[] bytes = new byte[this.readBuf.remaining()];
this.readBuf.get(bytes);
System.out.println("Server : " + new String(bytes).trim());
//6. 可以写回给客户端数据
} catch (IOException e) {
e.printStackTrace();
}
}
private void accept(SelectionKey key) {
try {
//1. 获取服务通道ServerSocketChannel
ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
//2. 获取客户端通道SocketChannel
SocketChannel sChannel = ssChannel.accept();
//3. 客户端通道SocketChannel也要设置成非阻塞模式
sChannel.configureBlocking(false);
//4 注册到多路复用器上,并设置读取标识
sChannel.register(this.selector, SelectionKey.OP_READ);
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、客户端
public static void main(String[] args) throws IOException {
SocketChannel sChannel = null;
try {
//1. 获取通道
sChannel = SocketChannel.open(
new InetSocketAddress("127.0.0.1", 8765));
//2. 切换成非阻塞模式
sChannel.configureBlocking(false);
//3. 分配缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
//4. 把从控制台读到数据发送给服务器端
Scanner scanner = new Scanner(System.in);
while (true) {
String str = scanner.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
buf.put((dateFormat.format(new Date()) + ":" + str).getBytes());
buf.flip();// 非阻塞模式下,write()方法在尚未写出任何内容时可能就返回了
while (buf.hasRemaining()) {
sChannel.write(buf);
}
buf.clear();
}
} finally {
sChannel.close();
}
}
ok,完成!!!
每天用心记录一点点。内容也许不重要,但习惯很重要!
NIO 源码分析(01) NIO 最简用法的更多相关文章
- NIO 源码分析(02-2) BIO 源码分析 Socket
目录 一.BIO 最简使用姿势 二.connect 方法 2.1 Socket.connect 方法 2.2 AbstractPlainSocketImpl.connect 方法 2.3 DualSt ...
- NIO 源码分析(02-1) BIO 源码分析
目录 一.BIO 最简使用姿势 二.ServerSocket 源码分析 2.1 相关类图 2.2 主要属性 2.3 构造函数 2.4 bind 方法 2.5 accept 方法 2.6 总结 NIO ...
- NIO 源码分析(05) Channel 源码分析
目录 一.Channel 类图 二.begin 和 close 是什么 2.1 AbstractInterruptibleChannel 中的 begin 和 close 2.2 Selector 中 ...
- NIO 源码分析(04) 从 SelectorProvider 看 JDK SPI 机制
目录 一.SelectorProvider SPI 二.SelectorProvider 加载过程 2.1 SelectorProvider 加载 2.2 Windows 下 DefaultSelec ...
- NIO 源码分析(03) 从 BIO 到 NIO
目录 一.NIO 三大组件 Channels.Buffers.Selectors 1.1 Channel 和 Buffer 1.2 Selector 1.3 Linux IO 和 NIO 编程的区别 ...
- 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器
1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...
- NIO源码分析:SelectionKey
SelectionKey SelectionKey,选择键,在每次通道注册到选择器上时都会创建一个SelectionKey储存在该选择器上,该SelectionKey保存了注册的通道.注册的选择器.通 ...
- [asp.net core 源码分析] 01 - Session
1.Session文档介绍 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档:https://docs.microsoft.com/zh-cn/aspnet/cor ...
- dubbo源码分析01:SPI机制
一.什么是SPI SPI全称为Service Provider Interface,是一种服务发现机制,其本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件.这样可以在运行时,动态为 ...
随机推荐
- selenium自动化测试时,chrome 出现“Chrome 正受到自动测试软件的控制”的解决办法
背景 使用selenium自动化测试的时候,启动浏览器出现'Chrome正在受到自动软件的控制'的问题,修改方法有两种. 一.在浏览器配置里加个参数,忽略掉这个警告提示语:disable_infoba ...
- MySQL高级学习笔记(五):查询截取分析
文章目录 慢查询日志 是什么 怎么玩 说明 查看是否开启及如何开启 默认 开启 那么开启了慢查询日志后,什么样的SQL才会记录到慢查询日志里面呢? Case 配置版 日志分析工具mysqldumpsl ...
- Centos7防火墙使用
修改时区 Centos7 #修改时区 timedatectl set-timezone Asia/Shanghai 开启防火墙 #添加一条规则 firewall-cmd --zone=public - ...
- git清理工作区
git clean -f 这将删除所有未被追踪的文件 git rev-list
- lzma 知识点滴
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xuweiwei1860/article/details/31419195 LZMA(Lempel-Z ...
- el-form-item内容过多,及弹窗框宽度属性show-overflow-tooltip设置
内容过多: :show-overflow-tooltip=true 宽度属性设置: .el-tooltip__popper{ max-width:30% }
- WPF textbox 鼠标滚动更新日期,text文本值更改
/// <summary> /// 选择日期 /// </summary> private void RQTxt_MouseWheel(object sender, Mouse ...
- teb教程5
跟随全局规划器 简介:本部分是关于如何配置局部规划器严格跟随全局规划,也包括调节在时优和路径跟随上的权衡. 1.先看一下via-points当前的优化行为:启动下面节点 roslaunch teb_l ...
- Linux下如何查看系统是多少位的
在Linux命令行下输入 getconf LONG_BIT 命令
- 39th 迷迷糊糊 二豆玩不转了
今天学的语法 1. # {} . format()的传送作用 请从键盘获取一个整数,求他的平方根,要求: 1 如果这个整数是大于等于0,则直接打印其平方根 2 否则, 打印其绝对值的平方根 x ...