Adjusted frame length exceeds 4096: 5637 - discarded 服务端解决

解决服务端Adjusted frame length exceeds 4096: 5637 - discarded问题
1. 具体错误
2. 错误原因
2.1 长度格式问题
2.2 长度大小问题
3. 解决方法
1. 具体错误
下面展示一些 错误详情。

io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 4096: 18247 - discarded
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:503)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:489)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.exceededFrameLength(LengthFieldBasedFrameDecoder.java:376)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:419)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:332)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:498)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:437)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)

2. 错误原因
2.1 长度格式问题
netty自带的LengthFieldBasedFrameDecodr长度编码协议,长度偏移取值必须为16进制表示。

new LengthFieldBasedFrameDecoder(4*1024,0,2);
// 长度位:000C 表示 12 长度
// eg:000C321453215435413415206753 (byte[]转16进制表示)

2.2 长度大小问题
调整后的帧长度超过4096:18247-已丢弃。本人在netty服务端对数据包进行处理时,使用了LengthFieldBasedFrameDecodr长度协议解码器。部分代码如下:

@Override
public void initChannel(SocketChannel ch) {
//长度协议的解码器
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(4*1024,0,2));
ch.pipeline().addLast(new SocketServerHandler());//控制类
}

而长度协议定一个最大可接收长度为4*1024 = 4096。故服务端接收超过4096个字节之后,服务端当做垃圾包丢弃了。

3. 解决方法
把netty服务端的长度协议的解码器最大可接收长度根据需求更改到足够长即可。

new LengthFieldBasedFrameDecoder(20*1024,0,2);
————————————

版权声明:本文为CSDN博主「大码的」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44055686/article/details/105945925

posted @ 2021-05-08 19:09  疯子110  阅读(6189)  评论(0编辑  收藏  举报