0

I need to create a server application using Netty which will allow multiple socket connections on the same port. That is, I need to be able to bind multiple ServerBootStrap objects to the same port. Is this possible using Netty? My code looks like this:

@Override
public void startServer() {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .option(ChannelOption.SO_REUSEADDR,true)
        .option(ChannelOption.SO_BACKLOG, 1)
        .option(ChannelOption.SO_KEEPALIVE, true)
        .option(ChannelOption.AUTO_CLOSE, false)
        .option(ChannelOption.TCP_NODELAY, true)
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch)
            throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new TWebMessageDecoder(logger));
            p.addLast(new StringDecoder(CharsetUtil.UTF_8));
            p.addLast(serverHandler);
        }
        });

    ChannelFuture f = b.bind(Utils.getPort(getConntype(), config))
        .sync();
    f.channel().closeFuture().sync();
} catch (Exception e) {
...
} finally {
...
}
}

I am getting the exception: Address already in use

  • 1
    No that's not possible, imho. Why would you want that anyway? what's the purpose of your service? – Herr Derb Aug 29 '16 at 13:56
  • agree, not possible. And i think this is not a "Netty" problem. – Jian Jin Aug 29 '16 at 15:54
  • 1
    You can't bind to the same port more than once. The [port unification example](https://github.com/netty/netty/tree/4.0/example/src/main/java/io/netty/example/portunification) may help you though. – Sean Bright Aug 30 '16 at 13:17

1 Answers1

0

It may be possible, but it depends on your underlying OS. To answer your question you actually need to understand what the option SO_REUSEADDR does and someone already did a great job explaining it here:

Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

Community
  • 1
  • 1
Leo Gomes
  • 1,033
  • 9
  • 14