Search code examples
javatcpnetty

Different response messages are merging while read from netty server


I have a netty client application. This app is connecting to a port and retrieving tcp response. When i read respose under load some response messages are concatenating. My request and response messages have a unique transaction key. I am following it for my business logic;

Actual: 000010690013200000101PMAX 567F 0000000000 1554092081842400 0 000010690013200000101PMAX 567F 0000000000 1556083801080400 0

Expected: 000010690013200000102PMAX 567F 0000000000 1554092081842400 0

Dublicate response is have 2 response but server send just 1 response we aggregated it while reading

@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
    String input = in.toString(CharsetUtil.UTF_8);
    log.info(input);
}

How can i resolve this issue and it is occuring under load.

Netty Version: 4.1.25.Final Java Version: OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

Thanks.


Solution

  • This is expected and just how TCP work. TCP has not notion of message boundaries and so you may receive the bytes just as you read them (which means stuff can be fragmented etc).

    If you need to preserve some sort of message boundaries you will need to encode this information in your protocol. For example you could prepend the length of the message and then use this information on the receiving peer to correct decode the message.

    Netty itself contains some decoders / encoders that

    LengthFieldPrepender

    LengthFieldBasedFrameDecoder