ServerSocket复用地址是否允许绑定到已经绑定的端口? [英] ServerSocket reuseAddress allow bind to an already bound port?

查看:87
本文介绍了ServerSocket复用地址是否允许绑定到已经绑定的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Netty 时,我感到惊讶的是,如果使用复用地址选项,它将允许ServerSocket绑定到相同的地址,而不会引发已经绑定异常"

When using Netty, I was surprised that if I use reuseAddress option, it allows a ServerSocket to bind to the same address without raising an "already bind 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允许新套接字重用关闭等待套接字,但这是不同的.以下是netstat命令的结果

I just thought that reuseAddress allows a new socket to reuse a close-wait socket, but this is different. The following is the result of a netstat command

  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

我错过了什么吗?发生了什么事?

Am I missing something? What's going on?

推荐答案

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

I assume that Windows allows this due to history. It is a bit of a security issue. See http://msdn.microsoft.com/en-us/library/ms740618 for some information about how the involved options interact. Which socket gets a connection is undefined. Maybe if you narrow down the version of Windows you are using you could narrow down what the response will be although it is probably just to not depend on it.

这篇关于ServerSocket复用地址是否允许绑定到已经绑定的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆