问题
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));
}
回答1:
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
来源:https://stackoverflow.com/questions/11525712/why-is-netty-giving-me-only-768-bytes-from-udp-messages