我是套接字编程的菜鸟。也许我在问一个基本问题。请多多包涵。我编写了一个示例 netty 服务器并从控制台启动它。它运行良好。我遇到的问题是,当我从两个控制台窗口运行同一服务器时,我希望其中一个会抛出“地址已在使用”异常。它不这样做,我不明白为什么。请帮忙。
public static void main(String[] args) {
ChannelFactory cf = new NioServerSocketChannelFactory(Executors.newFixedThreadPool(100), new MemoryAwareThreadPoolExecutor(1000,2048,25096,2,TimeUnit.SECONDS));
//ChannelFactory cf = new OioServerSocketChannelFactory(Executors.newFixedThreadPool(100), Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap(cf);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new ChannelHandler("test"));
}
});
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.reuseAddress", true);
bootstrap.setOption("child.connectTimeoutMillis", 30000);
//NEVER bootstrap.setOption("child.readWriteFair", true);
//bootstrap.setOption("disconnect", true);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("connectTimeoutMillis", 30000);
bootstrap.setOption("readWriteFair", true);
bootstrap.bind(new InetSocketAddress(9998));
}