java NIO 服务端例子2
所属分类 nio
浏览量 1011
连接 和 读 事件分别注册在不同的 Selector
读取数据后直接往 SocketChannel 里写
可直接写 ,不注册写事件
Selector serverSelector = Selector.open();
Selector clientSelector = Selector.open();
int port = 8000;
System.out.println("port="+port+","+LocalDateTime.now());
ServerSocketChannel listenerChannel = ServerSocketChannel.open();
listenerChannel.socket().bind(new InetSocketAddress(port));
listenerChannel.configureBlocking(false);
listenerChannel.register(serverSelector, SelectionKey.OP_ACCEPT);
BossThread bossThread = new BossThread(serverSelector,clientSelector);
bossThread.setName("bossThread");
bossThread.start();
WorkThread workThread = new WorkThread(clientSelector);
workThread.setName("workThread");
workThread.start();
完整代码
https://gitee.com/dyyx/hellocode/blob/master/src/nio/SimpleNioServer2.java
第一个 NIO server 例子
上一篇
下一篇
最简单的RPC框架实例
aerospike lua 构建有序列表
接口幂等几种实现
Spring容器初始化过程
中国诗词巅峰40首
使用_cat查看Elasticsearch状态