1
votes

I have written a netty server using http://netty.io/wiki/user-guide-for-4.x.html link. But I am getting data only upto 16384 bytes.

public class DiscardServerHandler extends ChannelInboundHandlerAdapter 
{   
    byte bNullArray[] = "".getBytes();
    String strFullData= new String(bNullArray,StandardCharsets.UTF_8);        
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception  
    { 
        try 
        {
           String MsgRead;
           ByteBuf in = (ByteBuf) msg;
           MsgRead=in.toString(io.netty.util.CharsetUtil.UTF_8);
           // here I get data only upto 1024 and this method get called 16 times.
           // So total data received is == 1024*16 = 16384        
           strFullData = strFullData + MsgRead;    
        } 
        finally 
        {
            ReferenceCountUtil.release(msg);
        }
    }    
    @Override 
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception 
    { 
        //WriteMyLog(strFullData);      
        //Here size of strFullData is 16384        
        strFullData = ProcessMyData(strFullData);       
        byte[] respByteBuf = strFullData.getBytes();                   
        ByteBuf Resp1 = ctx.alloc().buffer(respByteBuf.length);    
        Resp1.writeBytes(respByteBuf);                
        ctx.write(Resp1);            
        ctx.flush(); 
        ctx.close(); 
    }
}

How can I get more data?

1

1 Answers

1
votes

When your OS reads some data from socket it than passes it to user space (Java with netty in your case). 16 * 1024 is size of buffer that your OS reads from socket and than passes to you. This means ChannelInboundHandlerAdapter handler is not suitable for your case if your messages exceed this size. You need to use ByteToMessageDecoder. Something like :

public class MyBigMessageDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { 
        if (in.readableBytes() < MY_BIG_MESSAGE_SIZE) {
            return; 
        }

        out.add(in.readBytes(MY_BIG_MESSAGE_SIZE));
    }
}

Netty also has a bunch of ready handlers for different scenarios, like LineBasedFrameDecoder, LengthFieldBasedFrameDecoder, FixedLengthFrameDecoder, etc. I believe you can use some of them.

In general they all do the same - continue reading of income bytes until some condition met. And when ready - they pass read bytes further.