java InetSocketAddress 中的通配符地址是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11931175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:58:45  来源:igfitidea点击:

What does wildcard address in InetSocketAddress mean?

javasockets

提问by goh

In the docs for the constructor InetSocketAddress(int port)it says:

在构造函数的文档中,InetSocketAddress(int port)它说:

Creates a socket address where the IP address is the wildcard address and the port number a specified value.

创建一个套接字地址,其中 IP 地址是通配符地址,端口号是指定值。

What does a wildcard address do and what does it means when used in socket.bind()?

通配符地址的作用是什么,在 中使用时是什么意思 socket.bind()

回答by Werner Henze

From the docs: The wildcard is a special local IP address. It usually means "any" and can only be used for bind operations.

来自文档:通配符是一个特殊的本地 IP 地址。它通常表示“任何”并且只能用于绑定操作。

The value of this IP address is 0.0.0.0. If you have two network adapters, one with IP address 1.1.1.1 and one with IP address 2.2.2.2, then you can create a listening socket and bind it to 1.1.1.1 so that the socket will not bind to 2.2.2.2. You can also create a listening socket and bind it to 2.2.2.2, so that it will not bind to 1.1.1.1. If you do not care and want your socket to bind to all network cards, then you bind it to the wildcard address.

该 IP 地址的值为 0.0.0.0。如果您有两个网络适配器,一个 IP 地址为 1.1.1.1,另一个 IP 地址为 2.2.2.2,那么您可以创建一个侦听套接字并将其绑定到 1.1.1.1,这样套接字就不会绑定到 2.2.2.2。您也可以创建一个监听套接字并将其绑定到 2.2.2.2,这样它就不会绑定到 1.1.1.1。如果您不关心并希望您的套接字绑定到所有网卡,那么您可以将其绑定到通配符地址。

Another special value would be 127.0.0.1, meaning that only clients on the same computer could connect to your server.

另一个特殊值是 127.0.0.1,这意味着只有同一台计算机上的客户端才能连接到您的服务器。

回答by user2268854

A wildcard mask is a mask of bits that indicates which parts of an IP address can assume any value. In the Cisco IOS, they are used in several places, for example:

通配符掩码是位掩码,指示 IP 地址的哪些部分可以采用任何值。在 Cisco IOS 中,它们用于多个地方,例如:

  • To indicate the size of a network or subnet for some routing protocols, such as OSPF.
  • To indicate what IP addresses should be permitted or denied in access control lists (ACLs).
  • 指示某些路由协议(例如 OSPF)的网络或子网的大小。
  • 指示在访问控制列表 (ACL) 中应允许或拒绝哪些 IP 地址。