Kaleb Blue Kaleb Blue - 8 months ago 72
Java Question

Netty fragmentation for client messages with varying length

I'm having some problems with netty fragmentation of data.
My server accepts connection from client. The client
sends different message types. Theses messages tell the length of
the payload in the first 2 bytes.

An example:

Message 1
[00 21][21 bytes of payload]

Message 2
[00 25][23 bytes of payload]

Message 1
[00 80][80 bytes of payload]

I can tell I am missing messages because the messages are numbered sequentially.
Below is how the Payloads look after processing:

Count:1,Messnger:John Doe1, Message:"I can tell you something";
Count:1,Messnger:John Doe2, Message:"What do you have to tell me";
Count:1,Messnger:John Doe3, Message:"Always be reading and teaching";

So say if the client sends 5 messages:

Message 1
Message 2
Message 3
Message 4
Message 5

In my reading and processing, I miss some messages. I only get say

Message 1
Message 2
Message 4

I'm not sure why but I'm thinking messages might be fragmenting


It sounds like there might be a problem in how you are processing the messages. It is very possible that you are getting partial messages or even multiple messages in a single channelRead event.

I suggest looking into LengthFieldBasedFrameDecoder and adding that before your ServerHandler. It should help simplify message processing for you.