netty 的分隔符与定长解码器的使用
4种解码器应对TCP的粘包、拆包问题
new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline pl = ch.pipeline();
//pl.addLast(new LineBasedFrameDecoder(64)); //换行符解码
//pl.addLast(new FixedLengthFrameDecoder(3)); //固定长度解码
//消息以#作为分隔符,当达到单个消息的最大长度仍然没有查到分隔符,抛出TooLongFrameException
// ByteBuf delimiter = Unpooled.copiedBuffer("#".getBytes());
// pl.addLast(new DelimiterBasedFrameDecoder(16, delimiter));
pl.addLast(new SimpleDecoder());
pl.addLast(new SeverHandler());
}
LengthFieldBasedFrameDecoder 有点问题
public class SimpleDecoder extends LengthFieldBasedFrameDecoder {
private static final int FRAME_MAX_LENGTH = 64;
public SimpleDecoder() {
super(FRAME_MAX_LENGTH, 0, 4, 0, 4);
}
@Override
public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf frame = null;
try {
frame = (ByteBuf) super.decode(ctx, in);
if (null == frame) {
return null;
}
ByteBuffer byteBuffer = frame.nioBuffer();
//return RemotingCommand.decode(byteBuffer);
return decode(byteBuffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != frame) {
frame.release();
}
}
return null;
}
/*
public static RemotingCommand decode(final ByteBuffer byteBuffer) {
int length = byteBuffer.limit();
int oriHeaderLen = byteBuffer.getInt();
int headerLength = getHeaderLength(oriHeaderLen);
byte[] headerData = new byte[headerLength];
byteBuffer.get(headerData);
RemotingCommand cmd = headerDecode(headerData, getProtocolType(oriHeaderLen));
int bodyLength = length - 4 - headerLength;
byte[] bodyData = null;
if (bodyLength > 0) {
bodyData = new byte[bodyLength];
byteBuffer.get(bodyData);
}
cmd.body = bodyData;
return cmd;
}
public static String decode(final ByteBuffer byteBuffer) {
int length = byteBuffer.limit();
System.out.println("len="+length);
byte[] headerData = new byte[length];
byteBuffer.get(headerData);
String res=null;
try {
res = new String(headerData, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return res;
}
*/
public static Object decode(final ByteBuffer byteBuffer) {
int length = byteBuffer.limit();
int oriHeaderLen = byteBuffer.getInt();
System.out.println("decode..");
int headerLength = oriHeaderLen;
byte[] headerData = new byte[headerLength];
byteBuffer.get(headerData);
try {
String res = new String(headerData, "utf-8");
return res;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//RemotingCommand cmd = headerDecode(headerData, getProtocolType(oriHeaderLen));
/*
int bodyLength = length - 4 - headerLength;
byte[] bodyData = null;
if (bodyLength > 0) {
bodyData = new byte[bodyLength];
byteBuffer.get(bodyData);
}
cmd.body = bodyData;
*/
return null;
}
}
分享到:
相关推荐
netty分隔符和定长解码器的应用
netty分隔符和定长解码器的应用的小demo.
netty-frameDecoder换行符,自定义分隔符,定长度解码器说明使用tcp传送数据,由于缓存区大小的设置,MSS的tcp分段等因素,数据传输时会出现TCP粘包/拆包的问题。但是底层的tcp无法理解上层的业务数据,所以在底层也...
由于底层的 TCP 无法理解上层的业务数据, 所以在底层是无法保证数据包不被拆分和重 组的, 这个问题只能通过上层的应用协议栈...(2) 消息定长, 例如每个报文的大小为固定长度 200 字节, 如果不够, 空位补空格;
路径分隔符在编程中的注意事项
添加io.netty的maven库, 在com.zhao的包下,可以自行修改 有较多的注释,可以学习使用
这是一个小工具程序,主要功能是分隔符文本文件或定长文本文件的格式检查和简单加工,具体包括: 1) 文件格式的检查; 2)分页预览与编辑。提供文本视图和表格视图,在表格视图中可删除、插入、复制行或列。 3)...
socket网络编程,能够实现定长数据处理和变长数据处理。
本实训项目帮助学生理解定长指令周期三级时序系统的设计,能利用该时序构造硬布线控制器,支持5条典型MIPS指令在单总线CPU上运行,最终CPU能运行内存冒泡排序。 第1关MIPS指令译码器设计 第2关定长指令周期---时序...
第2关 定长指令周期---时序发生器FSM设计 第3关 定长指令周期---时序发生器输出函数设计 第4关 硬布线控制器组合逻辑单元 第5关 定长指令周期---硬布线控制器设计 第6关 定长指令周期---单总线CPU设计
对字符串按指定的字符串进行分隔,一段代码为知道分隔符数量的,一段代码为分隔符数量不知道。 对学习指针及字符串都有一定的帮助作用。
GIF文件结构与解码器纵观整个Gif结构, 我们看到两种快结构( Block Struct ) , 一种是定长,一种是那个. 定长: 1. Gif头 2. Gif 画布描述头, 英文叫逻辑屏幕标识符(Logical Screen Descriptor) . 3.Gif帧描述头 ...
2 定长指令周期---时序发生器FSM设计 3 定长指令周期---时序发生器输出函数设计 4 硬布线控制器组合逻辑单元 5 定长指令周期---硬布线控制器设计 6 定长指令周期---单总线CPU设计 全通关(放入logisim可查看电路,...
本源码实现可以在C#中使用的1D/2D编码解码器。条形码的应用已经非常普遍,几乎所有超市里面的商品上面都印有条形码;二维码也开始应用到很多场合,如火车票有二维码识别、网易的首页有二维码图标,用户只需要用手机...
第2关:定长指令周期---时序发生器FSM设计 第3关:定长指令周期---时序发生器输出函数设计 第4关:硬布线控制器组合逻辑单元 第5关:定长指令周期---硬布线控制器设计 第6关:定长指令周期---单总线CPU设计 1到6关...
用定长数组实现线性表用定长数组实现线性表
定长数据接收,windows网络编程,利用socket编程
JAVA XML \ 定长报文 与domino转换及通讯 参考链接 https://blog.csdn.net/chouyan1884/article/details/82870030
三菱PLC控制步进定长切断.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例三菱PLC控制步进定长切断.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例三菱PLC控制步进定长切断.zip三菱...
详细介绍了定长裁剪机的整机结构设计过程、多自由度机械手的位置安放与功能实现、气动回路设计等内容,采用三维虚拟技术对定长裁剪机的动作过程进行了模拟仿真,仿真结果表明:整机结构运行平稳,采用气缸驱动机械手完成...