'netty udp can not receive or send msg
I am using netty UDP as the IOT server of our company's equipment. But every few days, udpserver can no longer send and receive messages, and our equipment can't connect to the server.
Sometimes it'll take a while.
Sometimes it still doesn't work after a while, so you need to restart the server.
I don't know why. I guess is it because:
here bytebuf = packet copy().content();
the packet is too long ,cause the port 'cheat death'(can not receive or send msg),
How to limit the size of the packet?
Here is my code:
@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) {
ByteBuf buf = packet.copy().content();
// 大于1000 个字节不再处理 packet too long!
if (buf.capacity() > 1000) {
log.warn("收到过长的数据包 此包不再处理 packet too big ,discard");
return;
}
try {
byte[] b = new byte[buf.readableBytes()];
buf.readBytes(b);
// do sth....
}
catch (Exception e) {
log.error(e.getMessage(), e);
}
finally {
buf.release();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
