'System.Net.Sockets.TcpListener.TcpListener(int) is obsolete

D

Darwin

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
 
J

Jeroen Mostert

Darwin said:
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?
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.
 
G

Gilles Kohl [MVP]

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?

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

Darwin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top