当前位置: 首页 > news >正文

netty(二):NIO——处理可写事件

处理可写事件

什么情况下需要注册可写事件?

  • 在服务端一次性无法把数据发送完的情况下,需要注册可写事件
    • 服务端一次性是否能够把数据全部发送完成取决于服务端的缓冲区大小,该缓冲区不受程序控制

注册可写事件的步骤

  • 判断ByteBuffer是否仍有剩余,如果有剩余注册可写事件

    ByteBuffer bf = "hello client,welcome";
    SocketChannel sc = (SocketChannel) selectionKey.channel();
    sc.write(bf);
    if(bf.hasRemaining){selectionKey.interestOps(SelectionKey.OP_READ + SelectionKey.OP_WRITE);selectionKey.attachment(bf);
    }
    
  • 监听可写事件,判断数据是否写完,数据写完需要

    if (key.isWritable()){// 监听可写事件SocketChannel channel = (SocketChannel) key.channel();ByteBuffer byteBuffer = (ByteBuffer) key.attachment();channel.write(byteBuffer);if (!byteBuffer.hasRemaining()) {// 判断数据是否写完// 数据写完,解除对buffer的引用key.attach(null);// 数据写完,不再关注可写事件key.interestOps(key.interestOps() - SelectionKey.OP_WRITE);}
    }
    

代码示例

  • 客户端

    package com.ysf;import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    import java.nio.charset.Charset;public class Client {public static void main(String[] args) throws IOException {SocketChannel sc = SocketChannel.open();sc.connect(new InetSocketAddress("127.0.0.1",11027));sc.write(Charset.defaultCharset().encode("123456\n223456\nhello server\n"));while (true){ByteBuffer allocate = ByteBuffer.allocate(16);sc.read(allocate);allocate.flip();System.out.println(Charset.defaultCharset().decode(allocate));}}
    }
    
  • 服务端

    package com.ysf;import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.nio.charset.Charset;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;public class WriterSelectorServer {public static void handleContent(ByteBuffer byteBuffer){byteBuffer.flip();for (int i = 0;i < byteBuffer.limit();i++){if (byteBuffer.get(i) == '\n'){int length = i + 1 - byteBuffer.position();ByteBuffer allocate = ByteBuffer.allocate(length);for (int j = 0;j<length;j++){allocate.put(byteBuffer.get());}allocate.flip();System.out.println(Charset.defaultCharset().decode(allocate));}}byteBuffer.compact();}public static void main(String[] args) throws IOException {Selector selector = Selector.open();ServerSocketChannel ssc = ServerSocketChannel.open();ssc.configureBlocking(false);ssc.bind(new InetSocketAddress(11027));SelectionKey sscKey = ssc.register(selector, 0, null);sscKey.interestOps(SelectionKey.OP_ACCEPT);while (true){selector.select();Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()){SelectionKey key = iterator.next();iterator.remove();if (key.isAcceptable()){ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();SocketChannel sc = serverSocketChannel.accept();sc.configureBlocking(false);ByteBuffer welcome = Charset.defaultCharset().encode("welcome");sc.write(welcome);Map<String,ByteBuffer> attach = new HashMap<>();ByteBuffer readBuffer = ByteBuffer.allocate(16);attach.put("read",readBuffer);SelectionKey scKey = sc.register(selector, 0, attach);if (welcome.hasRemaining()){attach.put("write",welcome);scKey.interestOps(SelectionKey.OP_READ + SelectionKey.OP_WRITE);}else {scKey.interestOps(SelectionKey.OP_READ);}}else if (key.isReadable()){SocketChannel channel = (SocketChannel) key.channel();Map<String, ByteBuffer> attachment = ((Map<String,ByteBuffer>) key.attachment());ByteBuffer readBuffer = attachment.get("read");int read;try {read = channel.read(readBuffer);} catch (IOException e) {e.printStackTrace();key.cancel();continue;}if (read == -1){key.cancel();}else {handleContent(readBuffer);if (readBuffer.position() == readBuffer.limit()){ByteBuffer newAttachment = ByteBuffer.allocate(readBuffer.capacity() * 2);readBuffer.flip();newAttachment.put(readBuffer);attachment.put("read",newAttachment);}}}else if (key.isWritable()){SocketChannel channel = (SocketChannel) key.channel();Map<String, ByteBuffer> attachment = (Map<String, ByteBuffer>) key.attachment();ByteBuffer byteBuffer = attachment.get("write");channel.write(byteBuffer);if (!byteBuffer.hasRemaining()) {attachment.remove("write");key.interestOps(key.interestOps() - SelectionKey.OP_WRITE);}}}}}
    }
http://www.lryc.cn/news/142909.html

相关文章:

  • PHP基本语法解析与应用指南
  • ICS PA1
  • Java学数据结构(4)——散列表Hash table 散列函数 哈希冲突
  • OVRL-V2: A simple state-of-art baseline for IMAGENAV and OBJECTNAV 论文阅读
  • 【安全】原型链污染 - Hackit2018
  • net.ipv4.ip_forward=0导致docker容器无法与外部通信
  • 软考高级系统架构设计师系列论文九十八:论软件开发平台的选择与应用
  • Springboot整合WebFlux
  • uniapp 实现地图距离计算
  • 破除“中台化”误区,两大新原则考核中后台
  • 基于YOLOV8模型和Kitti数据集的人工智能驾驶目标检测系统(PyTorch+Pyside6+YOLOv8模型)
  • 基于Android的课程教学互动系统 微信小程序uniapp
  • OpenCV基础知识(9)— 视频处理(读取并显示摄像头视频、播放视频文件、保存视频文件等)
  • PostgreSQL命令行工具psql常用命令
  • 【CSS 画个梯形】
  • Spring Data Redis
  • 软件测试的方法有哪些?
  • Python Qt学习(二)Qt Designer
  • 我的数据上传类操作(以webDAV为例)
  • move与函数指针的简单使用
  • 第五章 树与二叉树 二、二叉树的定义和常考考点
  • 算法笔记/USACO Guide GOLD金组DP 1. Introduction to DP
  • 天锐绿盾安全U盘系统
  • 灰色预测模型
  • Yolo系列-yolov1
  • 单片机TVS/ESD二极管防护
  • TCP协议的重点知识点
  • 大数据——一文熟悉HBase
  • 如何有效进行RLHF的数据标注?
  • 2023年8月22日OpenAI推出了革命性更新:ChatGPT-3.5 Turbo微调和API更新,为您的业务量身打造AI模型