当前位置:网站首页>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();
}
边栏推荐
- 默认函数控制 =default 与 =delete
- [Shanda conference] definitions of some basic tools
- Runtime -- explore the nature of classes, objects, and classifications
- 6.GUI(图形,填充)
- jmeter关联登录302类型的接口
- Conversion between numeric types and strings
- SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
- Deploy odoo to the server and configure it as a service
- 84.(cesium篇)cesium模型在地形上运动
- Google Chrome small details
猜你喜欢

直播无顶流:董宇辉这么火,还有人看刘畊宏吗?

Pod type

过气剧本杀,被露营“复活”

Trust level of discover

SAP abap 数据类型,操作符和编辑器-02

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

84.(cesium篇)cesium模型在地形上运动

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

SAP ABAP 内部表:创建、读取、填充、复制和删除-06

北京恢复堂食半月记:如何重燃门店经营烟火气
随机推荐
POD 类型
SAP教程中的ALV报告 - ABAP列表查看器-012
Quick sort_ sort
Reddit对LaMDA模型的探讨:并非无状态,采用双重过程,相比它编辑维基百科的方式,有没有感情并不重要
LeetCode_回溯_动态规划_中等_131.分割回文串
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
杜老师自建国内不蒜子统计平台
学习量子纠缠的可解释表示,该深度生成模型可直接应用于其他物理系统
wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz
Swift -- save print log to sandbox
实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
转:杰克·韦尔奇:战略就是要少点沉思,敏于行动
Deploy odoo to the server and configure it as a service
'不敢去怀疑代码,又不得不怀疑代码'记一次网络请求超时分析
SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
【山大会议】私人聊天频道 WebRTC 工具类
[Shangshui Shuo series] day three - VIDEO
5.文件的读写(学生类)
Unity游戏优化(第2版)学习记录8
84. (cesium chapter) movement of cesium model on terrain