C# 如何监听多个IP地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1777629/
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
How to listen on multiple IP addresses?
提问by Erik Funkenbusch
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?
如果我的服务器分配了多个 IP 地址,并且我想收听其中的一些(或全部),我该怎么做?
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.
我是否需要为每个 IP 地址创建一个新的套接字并绑定它?我可以将多个 IP 地址绑定到单个套接字吗?IPAddress.Any 是否侦听所有 IP 地址?MSDN 库在这方面非常不清楚。
采纳答案by Matt Davis
You cannot bind a single socket to multiple endpoints. A SocketException
(invalid argument error) occurs the second time you call Bind()
for a given socket.
您不能将单个套接字绑定到多个端点。一个SocketException
(无效的参数错误)调用第二次Bind()
对一个给定的插座。
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.
正如其他人所说,您可以使用IPAddress.Any
侦听本地机器上的 IPv4 地址。但是,如果您只想侦听可用 IP 地址的子集,则必须创建单独的套接字。
回答by JonMR
Yes, IPAddress.Any will listen on all interfaces.
是的,IPAddress.Any 将侦听所有接口。
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any.aspx
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any.aspx
回答by Bevan
Technically, your server never has any IP addresses assigned to it.
从技术上讲,您的服务器永远不会分配任何 IP 地址。
Instead, individual network interfaces may be assigned IP addresses. Usually, each NIC gets one IP address, but that's just the most common case.
相反,可以为各个网络接口分配 IP 地址。通常,每个 NIC 都有一个 IP 地址,但这只是最常见的情况。
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.
如果要控制哪些接口正在侦听所选端口上的传入连接,则需要为每个接口创建一个单独的套接字。
回答by justincc
The MSDN library does seem contradcitory regarding IPAddress.Any. The Bind doc
MSDN 库在 IPAddress.Any 方面似乎有些矛盾。绑定文档
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx
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
说选择了“最合适”的地址,但 IPAddress.Any doc
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.any.aspx
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.
但是,有人告诉我这是正确的 IPAddress.Any 文档。
(adding this as an answer since I don't have enough rep to leave comments).
(添加此作为答案,因为我没有足够的代表发表评论)。
回答by For Guru
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
我已经研究过了,IPAddress.Any 不是正确的方法,它会绑定任何合适的 IP 地址。就我而言,我有 2 个 NIC,我无法解决问题。当我添加
System.Net.IPAddress ipAddress = IPAddress.Parse("xxx.xxx.xxx.xxx");
listener = new TcpListener(ipAddress, portNum);
It worked fine.
它工作得很好。
回答by Edward Brey
If you want to listen on all IPv4 and IPv6 addresses, use this code:
如果要侦听所有 IPv4 和 IPv6 地址,请使用以下代码:
var listener = new TcpListener(IPAddress.IPv6Any, port);
listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
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.
IPv6Any
告诉 Windows 侦听 IPv6 堆栈。将 socket 选项设置为 false 会告诉 Windows 不要将自己限制在 IPv6 堆栈上,而是还要侦听 IPv4 堆栈。默认值是仅在显式指定的堆栈上侦听。