Netty Bootstrap reuse for UDP

1,203 views
Skip to first unread message

tony....@anam.com

unread,
Sep 4, 2013, 11:48:18 AM9/4/13
to ne...@googlegroups.com
Hi,

I've been using Netty 4.0.8 and it works great for binding to multiple TCP ports but I was wondering if the same binding approach can be used for a UDP server.

There is a similar question on stackoverflow suggesting using one bootstrap per port but reusing the pool: http://stackoverflow.com/questions/18502735/bind-to-multiple-interface-but-not-all-0-0-0-0-in-udp 

The main code looks like this, modified from the QuoteOfTheMomentServer example here: 


EventLoopGroup group = new NioEventLoopGroup();

try {
    Bootstrap b = new Bootstrap();
    b.group(group)
    .channel(NioDatagramChannel.class)
    .option(ChannelOption.SO_BROADCAST, true)
    .handler(new UDPEmptyHandler());
    
    for (int i = 0; i < ports.size(); i++) {
        int port = ports.get(i);
        System.out.println("Binding to port " + port);
        b.bind(port).sync().channel().closeFuture().addListener(futureListenter);
        
    }
    //b.bind(port).sync().channel().closeFuture().await();
    } finally {
    group.shutdownGracefully();
}

Each time this runs it only seems to be able to bind on the first attempt and chokes on the second.

Here's the output:

Binding to port 80
Binding to port 81
Exception in thread "main" java.lang.IllegalStateException: channel not registered to an event loop
at io.netty.channel.AbstractChannel.eventLoop(AbstractChannel.java:106)
at io.netty.channel.nio.AbstractNioChannel.eventLoop(AbstractNioChannel.java:102)
at io.netty.channel.nio.AbstractNioChannel.eventLoop(AbstractNioChannel.java:41)
at io.netty.bootstrap.AbstractBootstrap.doBind0(AbstractBootstrap.java:317)
at io.netty.bootstrap.AbstractBootstrap.doBind(AbstractBootstrap.java:264)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:256)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:231)
at UDPServer.run(UDPServer.java:67)
at UDPServer.main(UDPServer.java:87)
04-Sep-2013 15:56:21 io.netty.util.concurrent.DefaultPromise notifyListener0
WARNING: An exception was thrown by UDPServer$1.operationComplete()
java.lang.ClassCastException: io.netty.channel.AbstractChannel$CloseFuture cannot be cast to io.netty.channel.group.ChannelGroupFuture
at UDPServer$1.operationComplete(UDPServer.java:47)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:621)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:548)
at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:389)
at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:82)
at io.netty.channel.AbstractChannel$CloseFuture.setClosed(AbstractChannel.java:821)
at io.netty.channel.AbstractChannel$AbstractUnsafe.close(AbstractChannel.java:541)
at io.netty.channel.nio.NioEventLoop.closeAll(NioEventLoop.java:543)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:662)
Java Result: 1

Any pointers would be appreciated and sorry if this is a really basic question.
Thanks
UDPServer.java
UDPEmptyHandler.java
Reply all
Reply to author
Forward
0 new messages