当前位置:网站首页>nio编程service
nio编程service
2022-06-22 15:10:00 【原力与你同在】
public static void main(String[] args) throws IOException {
// 创建select
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
// 注册 select和channel联系
// SelectionKey是事件发生后,可以得到事件和哪个channel的事件
// 四中事件
// accept -连接请求时触发
// connect -连接一旦建立触发
// read -可读事件
// write -可写事件
SelectionKey sscKey = ssc.register(selector, 0, null);
// 只关注accept事件
sscKey.interestOps(SelectionKey.OP_ACCEPT);
System.out.println("register key:"+sscKey);
ssc.bind(new InetSocketAddress(8888));
while (true){
// 没有事件,则阻塞线程,歇着;有事件才会恢复
selector.select();
// 处理事件,包含了所有发生的事件
// selector会向集合中添加key,但是不会主动删除事件
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
System.out.println("key:"+key);
if(key.isAcceptable()){
try {
// 事件不处理会一直加入事件
ServerSocketChannel channel = (ServerSocketChannel)key.channel();
// 没有连接建立会返回null
SocketChannel sc = channel.accept();
sc.configureBlocking(false);
ByteBuffer bf = ByteBuffer.allocate(2);
SelectionKey scKey = sc.register(selector, 0, bf);
scKey.interestOps(SelectionKey.OP_READ);
System.out.println("sc, "+sc);
// 一定要删除处理的事件
iterator.remove();
}catch (Exception e){
// 强制断开,关闭连接
key.cancel();
}
}else if(key.isReadable()){
try {
SocketChannel readChannel = (SocketChannel)key.channel();
ByteBuffer buffer1 = (ByteBuffer)key.attachment();
int read = readChannel.read(buffer1);
if(read == -1){
// 正常断开,也会触发一次读事件
key.cancel();
}else {
split(buffer1);
if(buffer1.position() == buffer1.limit()){
ByteBuffer newBuffer = ByteBuffer.allocate(buffer1.capacity()*2);
buffer1.flip();
newBuffer.put(buffer1);
key.attach(newBuffer);
}
}
iterator.remove();
}catch (Exception e){
}
}
}
}
}
private static void split(ByteBuffer source) {
source.flip();
for(int i=0;i<source.limit();i++){
//
if(source.get(i)=='\n'){
int length = i+1-source.position();
ByteBuffer target = ByteBuffer.allocate(length);
for(int j=0;j<length;j++){
target.put(source.get());
}
target.flip();
while (target.hasRemaining()){
char b = (char)target.get();
System.out.print(b);
}
System.out.println();
}
}
source.compact();
}
边栏推荐
- odoo本地文档功能开发记录
- 【山大会议】一些基本工具类定义
- Batch export excel zip using zipfile, openpyxl and flask
- Unity game optimization (version 2) learning record 8
- 【华为云至简致远】征文获奖名单出炉!
- String的模拟实现
- 信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强
- 首个赛博格人陨落背后:科技与渐冻症的极限赛跑
- SAP ABAP 中的用户出口和客户出口-015
- Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
猜你喜欢

Google Chrome small details

Bridging the gap between open source databases and database services

二叉树练习第二弹

Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance

SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010

B树和B+树

POD 类型

安全信得过!天翼云数据安全管理平台通过评测

谷歌浏览器的小细节

Make the text template in pycharm project support jinjia2 syntax
随机推荐
Deploy odoo to the server and configure it as a service
3.抽象類(shape)
C语言贪吃蛇
Oracle客户端和服务端的区别
GD32F4xx MCU 驱动mcp2515扩展CAN接口
Rosbag use command
全球首款AR隐形眼镜,元宇宙入口这次真的打开了?
期货怎么开户?网上期货开户安全吗?
1.类的继承(point)
数睿数据受邀参与南通企业数字化转型研讨会
[Shanda conference] use typescript to reconstruct the project
CMake教程系列-00-简介
Default function control =default and =delete
84.(cesium篇)cesium模型在地形上运动
【山大会议】一些基本工具类定义
ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务
jmeter关联登录302类型的接口
SAP ABAP 中的用户出口和客户出口-015
【LeetCode】9、回文数
SAP ABAP 中的 Smart Forms-014