本文介绍了的TcpListener:如何在所有接口上的特定端口听呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有三个重载构建一个 的TcpListener

There are three overloads for constructing a TcpListener:

  • 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 /非过时的方式与所有接口上的特定端口的在.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

推荐答案

只是绑定到<$c$c>IPAddress.Any - 这是多么这通常是......不知道,但它可能是你需要绑定到<$c$c>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参数和基础服务供应商将分配最合适的网络地址。

MSDN

IPAddress.Any 的现场

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

这篇关于的TcpListener:如何在所有接口上的特定端口听呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:54