如何监听多个IP地址?
如果我的服务器分配了多个 IP 地址,并且我想收听其中的一些(或全部)IP 地址,我该怎么做?
我需要为每个IP地址创建一个新的套接字并绑定它吗?我可以将多个 IP 地址绑定到一个套接字吗? IPAddress.Any 是否监听所有 IP 地址? MSDN库对这个问题非常不清楚。
If my server has multiple IP addresses assigned to it, and I would like to listen to some (or all) of them, how do I go about doing that?
Do I need to create a new socket for each IP address, and bind it? Can i bind multiple ip addresses to a single socket? Does IPAddress.Any listen on all IP addresses? The MSDN library is very unclear on this matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不能将单个套接字绑定到多个端点。第二次为给定套接字调用
Bind()
时,会发生SocketException
(参数无效错误)。正如其他人所说,您可以使用
IPAddress.Any
来监听本地计算机上的 IPv4 地址。但是,如果您只想侦听可用 IP 地址的子集,则必须创建单独的套接字。You cannot bind a single socket to multiple endpoints. A
SocketException
(invalid argument error) occurs the second time you callBind()
for a given socket.As others have said, you can use
IPAddress.Any
to listen to the IPv4 addresses on the local machine. However, if you only want to listen on a subset of the available IP addresses, you'll have to create separate sockets.从技术上讲,您的服务器从未分配过任何 IP 地址。
相反,可以为各个网络接口分配 IP 地址。通常,每个 NIC 都会获得一个 IP 地址,但这只是最常见的情况。
如果您想控制哪些接口正在侦听所选端口上的传入连接,则需要为每个接口创建一个单独的套接字。
Technically, your server never has any IP addresses assigned to it.
Instead, individual network interfaces may be assigned IP addresses. Usually, each NIC gets one IP address, but that's just the most common case.
If you want to control which interfaces are listening for incoming connections on your chosen port, you'll need to create a separate socket for each one.
如果要侦听所有 IPv4 和 IPv6 地址,请使用以下代码:
IPv6Any
告诉 Windows 侦听 IPv6 堆栈。将套接字选项设置为 false 告诉 Windows 不将自身限制在 IPv6 堆栈上,而是同时侦听 IPv4 堆栈。默认情况下仅侦听显式指定的堆栈。If you want to listen on all IPv4 and IPv6 addresses, use this code:
IPv6Any
tells Windows to listen on the IPv6 stack. Setting the socket option to false tells Windows to not limit itself to the IPv6 stack, but rather to also listen on the IPv4 stack. The default is to only listen on the stack explicitly specified.我已经研究过了,IPAddress.Any不是正确的方法,它会绑定任何合适的IP地址。就我而言,我有 2 个网卡,但无法解决该问题。当我添加
它时效果很好。
I have worked on it, IPAddress.Any is not the proper way, It will bind any Suitable IP address. In my case I have 2 NIC and I couldn't trouble shoot the problem. When I added
It worked fine.
是的,IPAddress.Any 将侦听所有接口。
http://msdn.microsoft.com/en-我们/library/system.net.ipaddress.any.aspx
Yes, IPAddress.Any will listen on all interfaces.
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any.aspx
MSDN 库对于 IPAddress.Any 似乎确实是矛盾的。绑定文档
http://msdn.microsoft。 com/en-us/library/system.net.sockets.socket.bind.aspx
表示选择了“最合适”的地址,但 IPAddress.Any 文档
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any .aspx
表示使用此常量,套接字必须侦听所有接口上的活动。
但是,我被告知这是 IPAddress.Any 正确的文档。
(添加此作为答案,因为我没有足够的代表来发表评论)。
The MSDN library does seem contradcitory regarding IPAddress.Any. The Bind doc
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx
says that the 'most suitable' address is chosen, but the IPAddress.Any doc
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any.aspx
says that with this constant the socket must listen for activity on all interfaces.
However, I'm told that it's the IPAddress.Any doc that is correct.
(adding this as an answer since I don't have enough rep to leave comments).