ServerSocketChannel+Selector实现多路复用io
发布日期:2021-06-28 19:59:39
浏览次数:3
分类:技术文章
本文共 4191 字,大约阅读时间需要 13 分钟。
import java.io.IOException;import java.net.Inet4Address;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.SocketAddress;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.Iterator;import java.util.Set;/** * Created by xs on 2018/2/8 * */public class SelectorServer { private static int LISTEN_PORT = 5300; public static void main(String[] args) { /* ServerSocketChannel ServerSocket SocketChannel Selector SelectionKey */ try { ServerSocketChannel ssc = buildServerSocketChannel(); Selector selector = Selector.open(); SelectionKey skey = ssc.register( selector, SelectionKey.OP_ACCEPT ); ByteBuffer echoBuffer = ByteBuffer.allocate(128); printSelectorKeys(selector); System.out.println("channel 准备就绪!"); while(true) { int num = selector.select();//获取通道内是否有选择器的关心事件 if (num < 1) { continue; } SetselectedKeys = selector.selectedKeys();//获取通道内关心事件的集合 Iterator it = selectedKeys.iterator(); while (it.hasNext()) { //遍历每个key SelectionKey key = it.next(); it.remove(); System.out.println("key hashCode=" + key.hashCode()); if (key.isAcceptable()) { // 有新的socket链接进来 ServerSocketChannel serverChanel = (ServerSocketChannel)key.channel(); SocketChannel sc = serverChanel.accept(); sc.configureBlocking( false ); SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ ); System.out.println( "Got connection from "+sc ); printSelectorKeys(selector); } if (key.isReadable()) { // 有请求进来 SocketChannel sc = (SocketChannel)key.channel(); System.out.println("address:" + sc.socket().getPort()); int bytesEchoed = 0; while((bytesEchoed = sc.read(echoBuffer))> 0){ System.out.println("bytesEchoed:"+bytesEchoed); } echoBuffer.flip(); if (bytesEchoed == -1) { System.out.println("connect finish!over!"); sc.close(); printSelectorKeys(selector); break; } byte [] content = new byte[echoBuffer.limit()]; echoBuffer.get(content); String result=new String(content, "utf-8"); doPost(result,sc); echoBuffer.clear(); } } } } catch (IOException e) { e.printStackTrace(); } } private static void printSelectorKeys(Selector selector) { Set keys = selector.keys(); Iterator iterator = keys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); System.out.println("all:selectKey hashCode=" + key.hashCode()); } } private static void doPost(String result, SocketChannel sc) { System.out.println(result); } private static ServerSocketChannel buildServerSocketChannel() throws IOException { ServerSocketChannel channel = ServerSocketChannel.open(); channel.configureBlocking( false );//使通道为非阻塞 ServerSocket ss = channel.socket();//创建基于NIO通道的socket连接 ss.bind(new InetSocketAddress(LISTEN_PORT));//新建socket通道的端口 //将NIO通道选绑定到择器,当然绑定后分配的主键为skey return channel; } private static void register(ServerSocketChannel ssc, Selector selector, int ops) throws ClosedChannelException { SelectionKey key = ssc.register(selector, ops); System.out.println("selectKey hashCode=" + key.hashCode()); }}
转载地址:https://blog.csdn.net/xxxxssss12/article/details/79311476 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月01日 02时45分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
用户画像标签体系包括哪些维度?有哪些应用场景?(附完整导图)
2019-04-29
收藏 | 大数据在阿里、滴滴、快手等公司的最佳实践!PDF限时下载
2019-04-29
数据仓库、数据湖、流批一体,终于有大神讲清楚了!
2019-04-29
KPI是怎样凑出来的?
2019-04-29
如何成为一个合格的数据架构师?
2019-04-29
收藏!一张图帮你快速建立大数据知识体系
2019-04-29
作为大数据技术面试官,我喜欢什么样的应届毕业生?
2019-04-29
数据分析报告,就该这么写
2019-04-29
元数据的生死时速
2019-04-29
一分钟读懂优秀PM与普通PM的区别
2019-04-29
如何更深刻的理解大数据(上)(附PPT下载)
2019-04-29
有一种信息化的死敌,叫数据打通!
2019-04-29
我被“非结构化数据包围了”,请求支援!
2019-04-29
用户画像,该怎么分析?
2019-04-29
数据团队的构成
2019-04-29
一文教你快速学习搭建属于自己的数据指标体系
2019-04-29
用户画像从0到100的构建思路
2019-04-29
一张图读懂数据分析
2019-04-29
你真的需要那么多报表么?
2019-04-29
一张图看懂信息化和数字化的本质区别
2019-04-29