java Netty 增加 ChannelBuffer 大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13086564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:26:20  来源:igfitidea点击:

Netty Increase ChannelBuffer Size

javanetty

提问by ukuta

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

你好,我有一个 Netty 服务器,它有一个应该接受字符串的处理程序。它似乎只接收最多 1024 字节的内容。如何增加缓冲区大小。我已经试过了

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();
}

}

}

回答by Nicholas

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

你在使用 UDP 吗?如果是这样,数据包将最大为 1024 字节。此代码注释在 QOTM 代码示例中:

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.

允许最大为 1024 字节的数据包(默认为 768)。您可以分别增加或减少此值以避免截断数据包或改善内存占用。

另请注意,无论您如何配置此选项,您的路由器都可能会截断或丢弃大型 UDP 数据包。在 UDP 中,如果数据包大于特定大小,则会被截断或丢弃,具体取决于路由器配置。IPv4 路由器会截断,而 IPv6 路由器会丢弃大数据包。这就是为什么在 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:

如果您使用 TCP,您应该在处理程序之前将帧解码器和字符串解码器添加到您的管道中;像这样的东西:

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.

请注意,您需要修改您的测试处理程序,因为 MessageEvent 实际上将包含您的字符串。

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

Make sense ?

说得通 ?

回答by Gaurav

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

在 4.0.10.Final 版本中,UDP 缓冲区大小设置为 2048 字节。

If You want to increase it set ChannelOptions as follows:

如果你想增加它设置 ChannelOptions 如下:

option(ChannelOption.SO_RCVBUF, int bytes)

and also

并且

option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(int Bytes))