Mark1234
Mark1234

Reputation: 619

How to read known number of bytes in Netty

I have a old style socket client that reads a report from server by doing below -

DataInputStream dis.....
int reportlength =  dis.readInt();
byte [] repBytes = new byte[reportlength ];
dis.read(repBytes , 0, reportlength );

How can i do similar in a Netty Client?

I am trying to make it work within below code but no success so far, as the read and read complete method gets called multiple time and i am not able to take out the report length part.

class CommunicationHandler extends ChannelInboundHandlerAdapter 
{

public void channelRead(ChannelHandlerContext ctx, Object obj) {
...
}
 public void channelReadComplete(ChannelHandlerContext ctx) {
....
}
}

Upvotes: 1

Views: 162

Answers (1)

trustin
trustin

Reputation: 12351

You can use LengthFieldBasedFrameDecoder to frame the inbound data properly. To learn why you get multiple channelRead() events for a single message, read the 'Dealing with stream transport' section in the official user guide: http://netty.io/wiki/user-guide-for-4.x.html#wiki-h3-11

Upvotes: 2

Related Questions