多次绑定到同一端口? [英] Binding multiple times to the same port?

查看:197
本文介绍了多次绑定到同一端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为什么不引发"java.net.BindException:地址已在使用中:JVM_Bind" 例外?

Why doesn't the following code throw a "java.net.BindException: Address already in use: JVM_Bind" exception?

import java.net.InetSocketAddress;
import java.net.ServerSocket;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        try (ServerSocket socket1 = new ServerSocket();
             ServerSocket socket2 = new ServerSocket();
             ServerSocket socket3 = new ServerSocket())
        {
            int port = 10000;

            socket1.setReuseAddress(false);
            socket1.bind(new InetSocketAddress("0.0.0.0", port));

            socket2.setReuseAddress(false);
            socket2.bind(new InetSocketAddress("127.0.0.1", port));

            socket3.setReuseAddress(false);
            socket3.bind(new InetSocketAddress("127.0.0.2", port));

            Thread.sleep(Long.MAX_VALUE);
        }
    }
}

随后运行"netstat"将显示:

Running 'netstat' afterwards displays:


C:\Users\Administrator>netstat -a -n | findstr 10000
  TCP    0.0.0.0:10000          0.0.0.0:0              LISTENING
  TCP    127.0.0.1:10000        0.0.0.0:0              LISTENING
  TCP    127.0.0.2:10000        0.0.0.0:0              LISTENING
  TCP    [::]:10000             [::]:0                 LISTENING

我正在Windows Server 2008 R2(64位)上运行此命令,并且'ipconfig/all'仅显示一个网络适配器/接口(其他网络适配器被禁用).但是,在其他一些计算机上,该程序实际上确实抛出了预期的"java.net.BindException:已在使用的地址:JVM_Bind"!

I am running this on Windows Server 2008 R2 (64-bit), and 'ipconfig /all' displays only one network adapter/interface (other network adapters are disabled). But, on some other machines, this program actually does throw the expected "java.net.BindException: Address already in use: JVM_Bind"!

可能会发生什么?

推荐答案

您可以在同一端口号上绑定不同的IP地址.操作系统可以通过传入的目标IP地址和TCP端口号来区分传入的数据包.

You can bind on the same port number on different IP addresses. The operating system can distinguish the incoming packets by their target IP address as well as their TCP port number.

操作系统为127.0.0.1:1000127.0.0.2:1000保留一个单独的serverSocket没问题.它知道每个数据包所属的位置-即使它打开了新的连接.

The operating system has no problem keeping a separate serverSocket for 127.0.0.1:1000 and 127.0.0.2:1000. It knows where each packet belongs - even if it opens a new connection.

请注意,IP地址0.0.0.0仅仅是-一个IP地址(在可以向其中发送IP数据包的意义上,它不是有效的 IP地址,但我对此无能为力对于0.0.0.0 == any).如果要侦听所有地址,请向InetSocketAddress提供null.此时,您更有机会获得所需的异常(除非操作系统决定通配符侦听的优先级较低,并且不真正与特定侦听重叠,因此它们可以将两者绑定).

Note that the IP address 0.0.0.0 is just that - an IP address (it is not a valid IP address in the sense IP packets could be sent there, but I can't any support for 0.0.0.0 == any either). If you want to listen on all addresses, supply null to the InetSocketAddress instead. At this point, you have a greater chance of getting the desired exception (unless the OS decides that wildcard listens have lower priority and don't really overlap specific listens, so they can bind both).

还请注意,设置setReuseAddress不会影响绑定.它仅影响某些拒绝或接受连接的细节.

Also note that setting setReuseAddress will not affect the binding. It only affects some specifics of what connections get refused or accepted.

作为旁注-等待三亿年的意义何在?

As a side note - what's the point of waiting three hundred million years?

这篇关于多次绑定到同一端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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