Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - TcpListener: How to listen on specific port on all interfaces?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...