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

丽水市龙泉市网站建设公司网站做301排名会掉

丽水市龙泉市网站建设公司,网站做301排名会掉,wordpress 自动分页插件,网站网页的书签怎么做处理可写事件 什么情况下需要注册可写事件? 在服务端一次性无法把数据发送完的情况下,需要注册可写事件 服务端一次性是否能够把数据全部发送完成取决于服务端的缓冲区大小,该缓冲区不受程序控制 注册可写事件的步骤 判断ByteBuffer是否仍…

处理可写事件

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

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

注册可写事件的步骤

  • 判断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.yayakq.cn/news/757749/

相关文章:

  • 58同城建网站怎么做wordpress更好后台登录logo
  • wordpress视频空白北京seo方法
  • 怎么做网站上做电子书别人做的网站怎么打开
  • 怎么用自己电脑做网站服务器吗网站建设与网页设计美食
  • 如何创建网站制作平台在线生成app免费
  • 爱站网关键词icp备案添加网站
  • 中山高端网站建设公司芜湖十大网络公司
  • 怎么做网站的分类目录网站专题制作 公司
  • 网站运营与管理正规网站建设公司多少钱
  • 营销型网站重要特点是?网上做网站赚钱
  • 本地网站建设多少钱信息大全琪恋网站建设
  • 网站开发协议百度06628网页制作与网站建设
  • 怎样做网站个人简介政务网站的建设方案
  • 专业购物网站免费网站在线观看
  • 建设一个行业性的网站价格网站建设客户分析调查问卷
  • 建站工具word购买云服务器后怎么做网站
  • 建设一个购物网站要多少钱中国建筑考试网入口
  • 网站手机网站制作网站开发 作品理念
  • 西安买公司的网站建设网站建设开题报告论述
  • 网站建设 协议书PS做网站页面尺寸
  • vip视频解析网站怎么做的网站开发到上线
  • 阿里云里面网站建设深圳微商城网站设计制作
  • 怎样搭建一个网站长沙求职网招聘网
  • 洛阳恒凯做的网站有哪些福安网站设计
  • 设计灵感的网站天河网站建设报价
  • 网站建设方案平台架构重庆无障碍网站建设
  • 广州网站推广自助wordpress 4.7.1下载
  • 德庆网站建设公司wordpress 破解主题下载地址
  • 环保类网站模板免费下载wordpress 远程图片本地化
  • 推荐几个看黄的网站免费手机端网站模板下载工具