简体   繁体   中英

Why is Netty giving me only 768 Bytes from UDP messages

I have set the "receiveBufferSize" option to 1024, but for some reason I'm still getting only 768 bytes in messageReceived. The header of the data indicates that size of the data being sent is 1004.

Below is the initialization code for the server:

public static void main(String[] args) throws Exception {
    ConnectionlessBootstrap b = new ConnectionlessBootstrap(new NioDatagramChannelFactory());

    // Options for a new channel        
    b.setOption("receiveBufferSize", 1024);
    System.out.println(b.getOptions());

    b.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new MyUDPPacketDecoder(),
                    new StdOutPrintHandler());
        }
    });

    b.bind(new InetSocketAddress(myPort));
}

You need to set an additional option - receiveBufferSizePredictorFactory.

in order to predict how much space it needs to allocate in order to hold the incoming message, netty uses a predictor that predicts the amount of byte to allocate.

there are two type of receive buffer size predictors, adaptive and fixed-size. the predictors are created by a predictor factory, which creates one for each channel created by the bootstrap.

if no predictor factory is set for the bootstrap (or no predictor is set manually for the channel), the channel uses the default 768 byte fixed-size predictor. all messages bigger then 768 bytes are cut down to that size.

you can add:

b.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024));

you can read about the predictors and their factories in netty documentation

ReceiveBufferSizePredictor Inteface

ReceiveBufferSizePredictorFactory Inteface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM