凡科网站是骗子,nginx安装wordpress,学会网站建设能成为一项职业吗,太平阳建设集团网站处理消息边界
为什么要处理边界
因为会存在半包和粘包的问题
1.客户端和服务端约定一个固定长度 优点#xff1a;简单 缺点#xff1a;可能造成浪费
2.客户端与服务端约定一个固定分割符
*缺点 效率低
3.先发送长度#xff0c;再发送数据
TLV格式#xff1a; type…处理消息边界
为什么要处理边界
因为会存在半包和粘包的问题
1.客户端和服务端约定一个固定长度 优点简单 缺点可能造成浪费
2.客户端与服务端约定一个固定分割符
*缺点 效率低
3.先发送长度再发送数据
TLV格式 type类型length长度Value数据类型和长度已知的情况下就可以方便获取消息大小
http1.1是TLV格式 http2.0是LTV格式
4.自动扩容解决消息边界问题 第一次read事件未能读完全部的输入那么会产生第二个读事件那么在第一次读的时候进行扩容 并复制之前的内容至新的buffer中 在第二个读事件触发以后使用扩容后的buffer读取剩余的数据
buffer应当和各自的channel绑定如何绑定需要用到附件attachment attachment需要在注册时放到selectionKey中。
// 绑定附件
SelectionKey scKey channel.register(selector,0,byteBuffer);// 获取附件
scKey.attachment();// 指定新的附件覆盖附件
scKey.attach(bytebuffer);示例代码
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.Iterator;public class BorderSelectorServer {/*** 读取到 \n时打印* param byteBuffer 缓冲字节*/public static void handle(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 j0;jlength;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();// 声明一个serverServerSocketChannel ssc ServerSocketChannel.open();ssc.bind(new InetSocketAddress(11027));ssc.configureBlocking(false);// 注册这个server到selectorSelectionKey sscKey ssc.register(selector, 0, null);// 添加sscKey关心的事件因为是serverChannel,所以应当关心accept事件sscKey.interestOps(SelectionKey.OP_ACCEPT);while (true) {selector.select();IteratorSelectionKey iterator selector.selectedKeys().iterator();while (iterator.hasNext()) {SelectionKey key iterator.next();iterator.remove();if (key.isAcceptable()) {ServerSocketChannel serverChannel (ServerSocketChannel) key.channel();SocketChannel socketChannel serverChannel.accept();socketChannel.configureBlocking(false);// 声明一个buffer缓冲区和socketChannel绑定ByteBuffer byteBuffer ByteBuffer.allocate(16);SelectionKey scKey socketChannel.register(selector, 0, byteBuffer);scKey.interestOps(SelectionKey.OP_READ);} else if (key.isReadable()) {// 当客户端异常断开链接是需要处理IOExceptiontry {SocketChannel channel (SocketChannel) key.channel();ByteBuffer byteBuffer (ByteBuffer) key.attachment();int read channel.read(byteBuffer);if (read -1) {// 客户端close()了key.cancel();}else{// 调用处理逻辑handle(byteBuffer);if (byteBuffer.position() byteBuffer.limit()){// buffer满了需要扩容ByteBuffer bufferEx ByteBuffer.allocate(byteBuffer.capacity() * 2);byteBuffer.flip();bufferEx.put(byteBuffer);key.attach(bufferEx);}}}catch (IOException e){
// e.printStackTrace();key.cancel();}}}}}
}
文章转载自: http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.xlmpj.cn.gov.cn.xlmpj.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn