java – Netty增加ChannelBuffer大小

您好我有一个Netty Server,其处理程序应该接受字符串.它似乎只接收最多1024个字节的内容.如何增加缓冲区大小.我已经尝试过了
bootstrap.setOption("child.sendBufferSize",1048576); 
bootstrap.setOption("child.receiveBufferSize",1048576);
@H_502_4@没有成功.

@H_502_4@处理程序如下

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

解决方法

你在使用UDP吗?如果是这样,数据包将以1024字节最大.此代码注释位于QOTM代码示例中:
@H_502_4@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.

@H_502_4@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.

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

pipeline.addLast("frameDecoder",new DelimiterBasedFrameDecoder(80960,Delimiters.lineDelimiter()));
pipeline.addLast("stringDecoder",new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("myHandler",new TestHandler());
@H_502_4@请注意,您需要修改测试处理程序,因为MessageEvent实际上将包含您的字符串.

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

相关文章

Netty实现httpserver简单示例 3个Java类实现最基本的接收请求,响应一个文本的简单http服务器。 https:...
Java NIO系列1 概观 Java NIO。中间的N你既可以理解为(new),也就是新的IO,相对于java1.5之前的IO它确...
关键字:使用Netty实现HTTP服务器,使用Netty实现httpserver,Netty Http server Netty是一个异步事件驱...
netty心跳机制示例,使用Netty实现心跳机制,使用netty4,IdleStateHandler 实现。Netty心跳机制,nett...
关键字:Netty开发redis客户端,Netty发送redis命令,netty解析redis消息, netty redis ,redis RESP协议...
前提 最近一直在看Netty相关的内容,也在编写一个轻量级的RPC框架来练手,途中发现了Netty的源码有很多...