ServerSocketChannel+Selector实现多路复用io
发布日期:2021-06-28 19:59:39
浏览次数:2
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月27日 06时57分05秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
攻防世界 Confusion1
2019-04-29
攻防世界 comment
2019-04-29
攻防世界 0ctf-unserialize(piapipia)
2019-04-29
攻防世界1-4
2019-04-29
攻防世界 4-8
2019-04-29
攻防世界 9-12
2019-04-29
BUUCTF The mystery of ip
2019-04-29
BUUCTF [De1CTF 2019]SSRF Me
2019-04-29
BUUCTF [极客大挑战 2019]FinalSQL
2019-04-29
[BJDCTF 2nd]简单注入
2019-04-29
buuctf [pasecactf_2019]flask_ssti
2019-04-29
Buuctf [第三章 web进阶]SSTI 20
2019-04-29
Buuctf [GKCTF2020]CheckIN
2019-04-29
CTFSHOW WEB入门 命令执行做题笔记(持续更新)
2019-04-29
应急响应流程
2019-04-29
Vulhub Flask SSTI漏洞复现
2019-04-29
CTFSHOW 文件包含
2019-04-29
Apache HTTPD 换行解析漏洞
2019-04-29
Vulhub Apache HTTPD 多后缀解析漏洞
2019-04-29
CTFshow 反序列化
2019-04-29