TcpListener:如何监听所有接口上的特定端口?
构建 TcpListener
需要三个重载:
public TcpListener(int端口);
(已过时)公共 TcpListener(IPEndPoint localEP)
public TcpListener(IPAddress localaddr, int port)
我想要侦听特定端口,但所有有一个重载可用于执行此操作,但它已被标记为已过时
。
为了提供帮助,IPEndPoint
is:
public IPEndPoint(
IPAddress address,
int port
)
这是第三个重载。以及 IPAddress
作为其构造函数:
- 一个
byte[]
- 一个
Int64
- 一个
byte[]
和一个Int64
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
.
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 anInt64
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需绑定到
IPAddress.Any
- 这通常是这样做的......不确定,但可能你需要绑定到IPAddress.IPv6Any
也是。这篇SO帖子建议您显式绑定到每个 IP 地址 - 并且这个 SO post 有关于如何获取所有 IP 地址的代码。 ..
来自 MSDN:
来自 MSDN:
IPAddress.Any 字段
Just bind to the
IPAddress.Any
- that's how this is usually done... not sure but it could be that you need to bind toIPAddress.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:
From MSDN:
IPAddress.Any Field