微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Netty增加ChannelBuffer大小

您好我有一个Netty Server,其处理程序应该接受字符串.它似乎只接收最多1024个字节的内容.如何增加缓冲区大小.我已经尝试过了
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代码示例中:

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.

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

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

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

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

合理 ?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐