简体   繁体   中英

Need to reassemble tcp packets with netty

currently I am working with an inhouse protocol where I send a request to our hardware and receive the answer with netty. In the message which I receive are several bytes which tell me how many bytes the answer will contain. In my channelRead method I wait until the readable bytes of the recieved message are equal or greater than the expected bytes to make sure I get all data.

if (((ByteBuf) msg).readableBytes() >= dataSize) {
        //do something with the bytes
        ctx.close();
        ((ByteBuf) msg).release();
}

This works fine if I receive exactly one tcp package from the hardware. Sometimes the hardware splits the TCP frame into several packages and my channelRead waits for ever.

Is there a simple way in netty to reassemble these packets in the channelRead method?

Just extend ByteToMessageDecoder. This will handle all the buffering for you. Check the javadocs for more details and an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM