ServerSocket reuseAddress允许绑定到已绑定的端口?

使用Netty时 ,我很惊讶如果我使用reuseAddress选项,它允许ServerSocket绑定到同一地址而不会引发“已绑定exception”

ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory(Executors .newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = pipeline(); p.addLast("handler", new DummyHandler()); return p; } }); bootstrap.setOption("reuseAddress", true); bootstrap.bind(new InetSocketAddress(2000)); bootstrap.bind(new InetSocketAddress(2000)); 

我只是认为reuseAddress允许新的套接字重用一个close-wait套接字,但这是不同的。 以下是netstat命令的结果

  C:\Users\secmask>netstat -a -n|grep 2000 TCP 0.0.0.0:2000 0.0.0.0:0 LISTENING TCP 0.0.0.0:2000 0.0.0.0:0 LISTENING 

我错过了什么吗? 这是怎么回事?

我认为Windows允许这是由于历史。 这有点安全问题。 有关所涉及选项如何交互的一些信息,请参见http://msdn.microsoft.com/en-us/library/ms740618 。 哪个套接字获取连接是未定义的。 也许如果你缩小你正在使用的Windows版本,你可以缩小响应范围,尽管它可能只是不依赖它。

你所看到的是reuseAddress应该做的事情。 无论状态如何,多个套接字可以同时绑定到同一个IP /端口。