Setting a server to listen on 8080 for incoming connections. Written in
VS2005 on a Multi-homed machine.
I get the warning:
Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete:
'This method has been deprecated. Please use TcpListener(IPAddress
localaddr, int port) instead.
on the code:
TcpListener tcpListener = new TcpListener(8080);
Do I really have to specify every IP address that I want to listen on?
The program compiled by changing the code to:
Int32 port = 8080;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener tcpListener = new TcpListener(localAddr, port);
But does it mean that I am only listening on 127.0.0.1:8080? Why do I have
to specify an IP address I thought that specifying a port automatically
meant that you would be listening on that port for all IP addresses?
Just want to understand why
Jeroen Mostert - 23 Mar 2008 12:06 GMT
> Setting a server to listen on 8080 for incoming connections. Written in
> VS2005 on a Multi-homed machine.
[quoted text clipped - 6 lines]
>
> Do I really have to specify every IP address that I want to listen on?
No. Use IpAddress.Any to listen on all local addresses.
> The program compiled by changing the code to:
>
> Int32 port = 8080;
>
> IPAddress localAddr = IPAddress.Parse("127.0.0.1");
This is clumsy; IPAddress.Loopback is a constant field for this. And yes,
this only listens on 127.0.0.1, so external hosts can't reach it.

Signature
J.
Gilles Kohl [MVP] - 23 Mar 2008 12:24 GMT
>Setting a server to listen on 8080 for incoming connections. Written in
>VS2005 on a Multi-homed machine.
[quoted text clipped - 18 lines]
>to specify an IP address I thought that specifying a port automatically
>meant that you would be listening on that port for all IP addresses?
Hmm, quoting from the docs:
"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. This might help simplify your application if
you have multiple network interfaces."
I found an interesting discussion about the potential rationale for the
deprecation here:
http://channel9.msdn.com/ShowPost.aspx?PostID=28931
Regards,
Gilles.
Darwin - 23 Mar 2008 19:19 GMT
I appreciate the response, when I look in the docs, there are 3 overloaded
constructor methods for tcpListener and it doesn't mention deprecation.
Anyway, I am back on track now.
Thanks
> Hmm, quoting from the docs:
>
[quoted text clipped - 13 lines]
> Regards,
> Gilles.