TcpListener: How to listen on a specific port on all interfaces?

There are three overloads for building a TcpListener :

I want to listen on a specific port, but on all available interfaces. Overload was available for this, but it was marked as obsolete .

What is the new preferred / not outdated way to listen on a specific port on all interfaces with TcpListener in .NET?


For ease of use, IPEndPoint :

 public IPEndPoint( IPAddress address, int port ) 

what is the third overload. And IPAddress takes as its constructor:

  • a byte[]
  • a Int64
  • a byte[] and Int64
+6
source share
1 answer

Just bind to IPAddress.Any - as is usually done ... not sure, but maybe you need to bind to IPAddress.IPv6Any .

This SO post assumes that you are attached to each IP address explicitly - and this SO post has a code on how to get all IP addresses ...


From MSDN :

If you do not need which local address is assigned, specify IPAddress.Any for the localaddr parameter, and the primary service provider will assign the most suitable network address.


From MSDN :

IPAddress.Any field

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

+19
source

Source: https://habr.com/ru/post/896177/


All Articles