服务器 定义解码器 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