J_HUNT
J_HUNT

Reputation: 1

What is the best approach to bind to different port on same handler in Netty?

Please review the below code. Here i am trying to listen two ports with same factory. Only one port listening right now. Please suggest how to achieve multiple port listening with same factory for same handler function for all ports.

public static void main(String[] args)
{
    ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newFixedThreadPool(1));
    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    ChannelPipelineFactory cpf = new ChannelPipelineFactory()
    {
        public ChannelPipeline getPipeline()
        {
            return Channels.pipeline(new testHandler());
        }
    };
    bootstrap.setPipelineFactory(cpf);
    bootstrap.setOption("child.tcpNoDelay", true);  
    ChannelGroup allChannels = new DefaultChannelGroup();
    Channel serverChannel = bootstrap.bind(new InetSocketAddress(5000));
    allChannels.add(serverChannel);
    Channel serverChannel1 = bootstrap.bind(new InetSocketAddress(6000));
    allChannels.add(serverChannel1);
    bootstrap.bind(new InetSocketAddress(5000));
}

Upvotes: 0

Views: 859

Answers (1)

liuzhengyang
liuzhengyang

Reputation: 406

You can create multiple ServerBootstrap instances. Each ServerBootstrap use a Server Channel to bind.

Upvotes: 1

Related Questions