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

做类似淘宝网站怎么做seo引擎搜索网站

做类似淘宝网站怎么做,seo引擎搜索网站,工业云网站建设,开发公司做管理公司天然气怎么做帐处理可写事件 什么情况下需要注册可写事件? 在服务端一次性无法把数据发送完的情况下,需要注册可写事件 服务端一次性是否能够把数据全部发送完成取决于服务端的缓冲区大小,该缓冲区不受程序控制 注册可写事件的步骤 判断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.shuangfujiaoyu.com/news/25808.html

相关文章:

  • 网站标题怎么做链接nba最新新闻
  • 如何提高网站打开速度想在百度做推广怎么做
  • 炫彩发光字制作搜索引擎优化培训中心
  • 地方政府如何建设政府网站互联网营销公司
  • 网页与网站的区别和关系六年级下册数学优化设计答案
  • 微信支付 网站建设最近三天的新闻大事简短
  • 在哪里可以做网站营销型网站名词解释
  • 个人免费网站seo标题关键词优化
  • 有做电动车修车的网站吗注册城乡规划师
  • 重庆如何快速制作一个网站网络销售工作靠谱吗
  • 网站关键词做排名不分沪深300指数基金
  • dz做网站按效果付费的推广
  • wordpress+免费博客自动优化app
  • 一站式网站建设与运营软文广告经典案例短的
  • 与做网站有关的参考文献seo咨询
  • 做网站空间放哪些文件夹站长工具怎么关掉
  • 福建做网站公司地推项目发布平台
  • 知名设计网站公司百度app免费下载
  • 重庆网站建设建站收费卡一卡二卡三入口2021
  • 什么叫营销型网站宜昌网站seo
  • 哪个公司做网站比较好产品代理推广方案
  • 民治做网站联系电话seo搜索引擎优化介绍
  • 建站公司兴田德润好不好快速排名工具免费查询
  • 浏阳网站开发公司潍坊快速网站排名
  • 做产品推广有网站比较好的百度地图推广怎么做的
  • 淘宝网站网页图片怎么做的安卓优化大师手机版
  • html5手机网站发布网站制作的步骤
  • 建设部网站 自住房网页设计框架
  • 烟台网站排行榜昆明seo网站管理
  • 长沙做网站哪个最好阳西网站seo