构造 TcpListener
有三个重载:
public TcpListener(int port);
(已过时)public TcpListener(IPEndPoint localEP)
public TcpListener(IPAddress localaddr, int port)
我想监听一个特定的端口,但是在 全部 可用的接口(interface)。有一个重载可用,但它被标记为
obsolete
.使用
TcpListener
在所有接口(interface)上监听特定端口的新首选/非过时方法是什么?在.NET 中?为了帮助起见,
IPEndPoint
是:public IPEndPoint(
IPAddress address,
int port
)
这就是第三个重载。还有一个
IPAddress
接受,作为它的构造函数:byte[]
Int64
byte[]
和 Int64
最佳答案
只需绑定(bind)到 IPAddress.Any
- 这就是通常这样做的方式...不确定,但可能是您需要绑定(bind)到 IPAddress.IPv6Any
也。
这个SO post建议您明确绑定(bind)到每个 IP 地址 - 而这个 SO post有关于如何获取所有 IP 地址的代码...
来自 MSDN :
If you do not care which local address is assigned, specify
IPAddress.Any
for the localaddr parameter, and the underlying service provider will assign the most appropriate network address.
来自 MSDN :
IPAddress.Any 字段
Provides an IP address indicating that the server should listen for client activity on all network interfaces.
关于.net - TcpListener:如何监听所有接口(interface)上的特定端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7238096/