当前位置:网站首页>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();
}
边栏推荐
- B树和B+树
- [译文] 弥合开源数据库和数据库业务之间的鸿沟
- 默认函数控制 =default 与 =delete
- String的模拟实现
- Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
- 数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
- 【LeetCode】9、回文数
- Jenkins automatically triggers compilation by checking code submissions
- 什么是 SAP ABAP? 类型、ABAP 完整形式和含义
- 首个赛博格人陨落背后:科技与渐冻症的极限赛跑
猜你喜欢

odoo本地文档功能开发记录

SAP ABAP 表控制与示例-07

实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)

B树和B+树

84. (cesium chapter) movement of cesium model on terrain

音视频基础知识|ANS 噪声抑制原理解析

SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013

Ironsource Luna offers a limited time discount for Apple search ads and enjoys 3 months of free service upon registration

首个赛博格人陨落背后:科技与渐冻症的极限赛跑

Google Chrome small details
随机推荐
【山大会议】项目引入 Redux
SAP ABAP 中的模块化:宏、子程序和功能模块 -04
SAP价值流程&帮助请求流程-011
How to embody the value of knowledge management in business
Maze problem (BFS record path)
phantomJs使用总结
Solve the problem of MySQL remote login permission
用递归法求Fibonacci数列第n项的值
[Shanda conference] project introduces Redux
【华为云至简致远】征文获奖名单出炉!
【山大会议】私人聊天频道 WebRTC 工具类
Navicat premium connecting to Oracle database (Graphic tutorial)
C语言贪吃蛇
SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
【山大会议】注册页的编写
Pymssql Module User Guide
Program substitution function
[Shanda conference] use typescript to reconstruct the project
Static assertion static_ assert