请查看以下代码。在这里,我试图听同一工厂的两个端口。现在只有一个端口正在收听。对于所有端口,请建议如何使用相同的工厂实现多端口监听,以实现相同的处理函数。
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));
}
答案 0 :(得分:1)
您可以创建多个ServerBootstrap实例。每个ServerBootstrap都使用服务器通道进行绑定。