TcpListener:如何监听所有接口上的特定端口?

发布于 2024-12-01 19:09:26 字数 1374 浏览 6 评论 0原文

构建 TcpListener 需要三个重载:

想要侦听特定端口,但所有有一个重载可用于执行此操作,但它已被标记为已过时

使用 .NET 中的TcpListener


为了提供帮助,IPEndPoint is:

public IPEndPoint(
    IPAddress address,
    int port
)

这是第三个重载。以及 IPAddress 作为其构造函数:

  • 一个 byte[]
  • 一个 Int64
  • 一个 byte[] 和一个 Int64

There are three overloads for constructing a TcpListener:

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.

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


For helpfulness sake, an IPEndPoint is:

public IPEndPoint(
    IPAddress address,
    int port
)

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

  • a byte[]
  • an Int64
  • a byte[] and an Int64

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-12-08 19:09:26

只需绑定到 IPAddress.Any - 这通常是这样做的......不确定,但可能你需要绑定到 IPAddress.IPv6Any 也是。

这篇SO帖子建议您显式绑定到每个 IP 地址 - 并且这个 SO post 有关于如何获取所有 IP 地址的代码。 ..


来自 MSDN

如果您不关心分配哪个本地地址,请为 localaddr 参数指定 IPAddress.Any,底层服务提供商将分配最合适的网络地址。


来自 MSDN

IPAddress.Any 字段

提供一个 IP 地址,指示服务器应侦听所有网络接口上的客户端活动。

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.

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...


From 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.


From MSDN:

IPAddress.Any Field

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文