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