About UDP server setting in netty

2019-07-27 10:44发布

问题:

Now I am implementing UDP server to gather sflow datagrams using netty library.

Here is my setting for udp server.

ChannelFactory factory = new NioDatagramChannelFactory(this.threadPool.getScheduledExecutor());
    ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(factory);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory(){
        public ChannelPipeline getPipeline(){
            ChannelPipeline lChannelPipeline = Channels.pipeline();
            lChannelPipeline.addLast("sflowmessagedecoder", new SflowMessageDecoder());
            lChannelPipeline.addLast("handler", new SflowCollectorHandler());

            return lChannelPipeline;
        }
    });

    bootstrap.setOption("receiveBufferSize", 65536);
    bootstrap.bind(new InetSocketAddress(port));

The problem is that when I receive a sflow datagram (about 1300 bytes long, this can be checked using wireshark), netty only reads about 700 bytes, this can be checked by channelbuffer's size, of the datagram from the channelbuffer.

However, I use java.net.* libraries such as DatagramSocket.class and DatagramPacket.class, it receives sflow datagram correctly (about 1300 bytes).

Somebody can help me? Due to this, I cannot parse sflow datagram correctly.

Thanks.

回答1:

I had the same Issue, try using Oio and not Nio! (simply change "nio" to "oio" and "Nio" to "Oio".

http://lists.jboss.org/pipermail/netty-users/2009-June/000891.html

Now it's working ;)



回答2:

I have not tested this out, but give it a try, could work for you.

org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory bufferSizePredictor = new FixedReceiveBufferSizePredictorFactory(1300);// or whatever size you require.
serverBootstrap.setOption("receiveBufferSizePredictorFactory",
            bufferSizePredictor);

Once you set this predictor factory it might help.



标签: netty