6

Hello I have a Netty Server with a handler that should accept strings. It seems to only receive content up to 1024 bytes. How can i increase Buffer size. I have already tried

bootstrap.setOption("child.sendBufferSize", 1048576); 
bootstrap.setOption("child.receiveBufferSize", 1048576);

without success.

The handler is as below

public class TestHandler extends SimpleChannelHandler {


@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

    ChannelBuffer buf = (ChannelBuffer) e.getMessage();

    String response = "";

    if (buf.readable()) {

        response = buf.toString(CharsetUtil.UTF_8);
        System.out.println("CONTENT: " + response);
    }

    System.out.println(response);


}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
    e.getCause().printStackTrace();

    Channel ch = e.getChannel();
    ch.close();
}

}

ukuta
  • 371
  • 2
  • 4
  • 14

2 Answers2

10

Are you using UDP ? If so, packets will max out at 1024 bytes. This code comment is in the QOTM code sample:

Allow packets as large as up to 1024 bytes (default is 768). You could increase or decrease this value to avoid truncated packets or to improve memory footprint respectively.

Please also note that a large UDP packet might be truncated or dropped by your router no matter how you configured this option. In UDP, a packet is truncated or dropped if it is larger than a certain size, depending on router configuration. IPv4 routers truncate and IPv6 routers drop a large packet. That's why it is safe to send small packets in UDP.

If you are using TCP, you should add a frame decoder and a string decoder into your pipeline before yout handler; Something like this:

pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(80960, Delimiters.lineDelimiter()));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("myHandler", new TestHandler());

Mind you, you will need to modify your test handler because the MessageEvent will actually contain your string.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    String response = (String) e.getMessage();
    System.out.println(response);
}

Make sense ?

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • Thanks Nicholas. Now I notice that the last line is not printed.Why? – ukuta Oct 26 '12 at 16:51
  • Good question. Can you put a full end-of-line delimiter at the end of the last line ? – Nicholas Oct 26 '12 at 17:12
  • 1
    Thanks Nicholas. It is possible to use my own delimeter. Brilliant!!! This is a MUST-READ tutorial by Nicholas. http://seeallhearall.blogspot.kr/2012/06/netty-tutorial-part-15-on-channel.html... p.addLast("frameDecoder", new DelimiterBasedFrameDecoder(8192, ChannelBuffers.wrappedBuffer("".getBytes()))); – ukuta Oct 27 '12 at 15:45
  • 1
    UDP packets do have a default max size of 768, but if you increased max size does not max out at 1024. the max size can be increase as detailed in the following post: [Why is Netty giving me only 768 Bytes from UDP messages](http://stackoverflow.com/questions/11525712/why-is-netty-giving-me-only-768-bytes-from-udp-messages) – Yoav Slomka Nov 01 '12 at 14:04
5

In version 4.0.10.Final for UDP Buffer size is set to 2048 bytes.

If You want to increase it set ChannelOptions as follows:

option(ChannelOption.SO_RCVBUF, int bytes)

and also

option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(int Bytes))
user229044
  • 232,980
  • 40
  • 330
  • 338
Gaurav
  • 51
  • 1
  • 2
  • The location of this has changed slightly since 4.0.10, `channel().config().setOption( ChannelOption.SO_RCVBUF, numOfBytes );` Thanks, cheers. – Matt Clark Dec 15 '15 at 23:05