netty的LengthFieldBasedFrameDecoder 粘包问题

hhd715213471 2015-06-11 08:51:25
服务器 定义解码器 super(4096, 0, 2, 0, 0);

客户端
正常情况 :
String host = "127.0.0.1";
int port = 8586;
Socket client = new Socket(host, port);
DataOutputStream dos = new DataOutputStream(client.getOutputStream());

while (!client.isClosed()){
dos.writeShort(2); //输出总长度
dos.writeShort(0x0001); //命令
dos.flush();
Thread.sleep(3000);
}
dos.close();
client.close();

粘包情况:
String host = "127.0.0.1";
int port = 8586;
Socket client = new Socket(host, port);
DataOutputStream dos = new DataOutputStream(client.getOutputStream());

while (!client.isClosed()){
dos.writeShort(3); //输出总长度
dos.writeShort(0x0001); //命令
dos.writeShort(5); //命令
dos.flush();
Thread.sleep(3000);
}
dos.close();
client.close();
这样第一次发是没有问题的,第二次服务器就报错了,以下是错误信息:
io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 4096: 25346 - discarded
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:501) ~[netty-all-4.0.23.Final.jar:4.0.23.Final]
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:477) ~[netty-all-4.0.23.Final.jar:4.0.23.Final]
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:403) ~[netty-all-4.0.23.Final.jar:4.0.23.Final]
...全文
2153 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dongft 2017-05-08
  • 打赏
  • 举报
回复
求解决方案..
David_New 2017-03-09
  • 打赏
  • 举报
回复
求解决方案..
lingwu7 2016-03-22
  • 打赏
  • 举报
回复
怎么解决的呢?我也遇到这个问题了,好苦恼啊
pusu2009 2015-11-17
  • 打赏
  • 举报
回复
怎么解决的这个问题,我也遇到了相似的问题,指教一下
SaintTinyBoy 2015-08-07
  • 打赏
  • 举报
回复
引用 1 楼 hhd715213471 的回复:
- -我自己解决了。
解决方案是什么,我也碰到这问题了
hhd715213471 2015-06-12
  • 打赏
  • 举报
回复
- -我自己解决了。

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧