净增加ChannelBuffer大小 [英] Netty Increase ChannelBuffer Size

查看:138
本文介绍了净增加ChannelBuffer大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个带有处理程序的Netty Server,该处理程序应该接受字符串.似乎最多只能接收1024个字节的内容.我如何增加缓冲区大小.我已经尝试过

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

没有成功.

处理程序如下

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

}

推荐答案

您是否正在使用UDP?如果是这样,则数据包将最大为1024字节.此代码注释在QOTM代码示例中:

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

允许的数据包最大为1024个字节(默认值为768).你可以 增大或减小此值,以避免数据包被截断或 分别提高内存占用量.

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.

还请注意,较大的UDP数据包可能会被截断或丢弃 由路由器决定,无论您如何配置此选项.在UDP中, 如果数据包大于特定大小,则该数据包将被截断或丢弃, 取决于路由器配置. IPv4路由器截断和IPv6 路由器会丢弃一个大数据包.这就是为什么可以安全发送小笔钱的原因 UDP中的数据包.

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.

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

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

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

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

有道理吗?

这篇关于净增加ChannelBuffer大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆