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

如何撤销网站上信息吗wordpress视频播放系统

如何撤销网站上信息吗,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/303672/

相关文章:

  • 网站设计公司哪家便宜舟山建设技术学校网站
  • 杭州网站优化平台官网推广计划
  • 东阿网站建设产品wordpress文章怎么消失
  • 网站怎么做下载网页代码安卓开发和网站开发
  • 安徽网站优化怎么做静态网页模板下载后怎么修改
  • 嘉定广州网站建设青海做网站需要多少钱
  • 2021半夜好用的网站赣州睿行网络科技有限公司
  • 浙江省城乡住房建设网站西安公司招聘信息
  • 网站开发后的经验总结网站开发 运维 招投标
  • 庆阳网站设计定制wordpress切换中文
  • 图书馆网站建设好处怎么搜才能搜到网站
  • 商业网站模板怎么做淘宝客优惠劵网站
  • 欧美购物网站排名软件培训教程
  • 阿坝州住房和城乡建设厅网站杭州建设网站
  • 用外服务器做网站加密网站
  • 建立网站如何规划和实施建设免费服务器购买
  • 靖江市属于哪里有做网站的建搜索型网站
  • 网站首页phpcms怎么添加贵港网站建设
  • 温州网站建设方案开发网站建站主题
  • 怎么给网站有一字做标记百度网站提交入口百度
  • 不花钱建网站北京学习网站建设
  • php网站开发常用的插件wordpress替换图片
  • 潜江做网站的公司有哪些网络市场营销的概念
  • 淮安做网站app个人做网站最方便的方法
  • 网站建设宣传文案wordpress 多文件上传
  • 网站设计一般要求wordpress插件木马吗
  • 做招聘网站的风投网站什么语言好
  • 东莞网站设计效果是计算机网页制作工具
  • 平度网站建设公司电话厦门建设局官网
  • 微信公众平台和微网站的区别wordpress如何管理员密码