的TcpListener:如何在所有接口上的特定端口听呢?端口、接口、如何在、TcpListener

由网友(霸气而又不可高攀的帅比)分享简介:有三个重载构建一个 的TcpListener :There are three overloads for constructing a TcpListener:公开的TcpListener(INT端口); (过时的)公开的TcpListener(IPEndPoint localEP) 公开的TcpListen...

有三个重载构建一个 的TcpListener

There are three overloads for constructing a TcpListener:

公开的TcpListener(INT端口); (过时的) 公开的TcpListener(IPEndPoint localEP) 公开的TcpListener(ip地址localaddr,INT口) public TcpListener(int port); (obsolete) public TcpListener(IPEndPoint localEP) public TcpListener(IPAddress localaddr, int port)

我的需要的监听某个特定的端口,但在所有可用的接口。这里的已可用过载要做到这一点,但它被标记为过时

i want to listen on a particular port, but on all available interfaces. There was an overload available to do that, but it's been marked as obsolete.

什么是新的preferred /非过时的方式与所有接口上的特定端口的 的TcpListener 在.NET?

What is the new preferred/non-obsolete way to listen on a particular port on all interfaces with a TcpListener in .NET?

有关乐于助人的缘故,一个 IPEndPoint 是:

For helpfulness sake, an IPEndPoint is:

public IPEndPoint(
    IPAddress address,
    int port
)

这是第3次超载是什么。而一个 ip地址 需要,因为它的构造函数:

which is what the 3rd overload is. And an IPAddress takes, as its constructor:

字节[] 的Int64 字节[]和的Int64 a byte[] an Int64 a byte[] and an Int64

推荐答案

只是绑定到IPAddress.Any - 这是多么这通常是......不知道,但它可能是你需要绑定到IPAddress.IPv6Any了。

Just bind to the IPAddress.Any - that's how this is usually done... not sure but it could be that you need to bind to IPAddress.IPv6Any too.

这SO帖子建议您绑定到每个IP addresse明确 - 这SO帖子对如何获得code所有IP ...不会忽略

This SO post suggests that you bind to every IP addresse explicitly - and this SO post has code on how to get all IP adresses...

从 MSDN :

如果你不关心它的本地地址分配,指定 IPAddress.Any 的localaddr参数和基础服务供应商将分配最合适的网络地址。

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 的现场

提供指示该服务器应该监听客户端活动在所有网络接口的IP地址。

Provides an IP address indicating that the server should listen for client activity on all network interfaces.

阅读全文

相关推荐

最新文章