Problems with TCP Listener

J

Jerry Spence1

I have been looking at examples of code to create a simple TCP listener on a
certain port.

However they all start off with this line:

Dim tcpListener As New TcpListener(portNumber)

When I code this, I get an error which says that this is obsolete and I
should use tcpListener(IPAddress localaddr int port)

The examples I am looking at are only a few weeks old so I don't understand
why it is obsolete.

I am actually trying to listen for data on port 10002 from a network device
192.168.0.61.

I tried:
Dim ipAddress As ipAddress = Dns.Resolve("localhost").AddressList(0)
Dim tcpListener As New TcpListener(ipAddress,10002)
Dim Listener As New System.Net.Sockets.TcpListener(ipAddress, 10002)
Listener.Start()
Dim tcpClient As tcpClient = Listener.AcceptTcpClient()

but it just sits there and doesn't see any connections coming in.

On the sender side it is sending a short text message on port 10002 to host
192.168.0.2, which is my PC. Is this the same as 127.0.0.0 as defined in the
line:

Dim ipAddress As ipAddress = Dns.Resolve("localhost").AddressList(0)

above?

Thanks

-Jerry
 
S

Steven Nagy

localhost, or loopback, should probably be 127.0.0.1
But if you have a local network then just use their IPs, such as your
192.168.0.2 example.

I think you might be using examples for VS2003 in VS2005?
In your code you have declared two Listeners, but you should only have
1 listener per port/ip address.
Have it sit in an infinate loop calling AcceptSocket (well, thats in
VS2003, not sure about 2005).

Also, make sure you have your PC firewall configured to allow that port
through, on BOTH machines.
This could be your headache (windows firewall by default will be
blocking this port).
 
T

Tom Shelton

Jerry said:
I have been looking at examples of code to create a simple TCP listener on a
certain port.

However they all start off with this line:

Dim tcpListener As New TcpListener(portNumber)

When I code this, I get an error which says that this is obsolete and I
should use tcpListener(IPAddress localaddr int port)

The examples I am looking at are only a few weeks old so I don't understand
why it is obsolete.

I am actually trying to listen for data on port 10002 from a network device
192.168.0.61.

I tried:
Dim ipAddress As ipAddress = Dns.Resolve("localhost").AddressList(0)
Dim tcpListener As New TcpListener(ipAddress,10002)
Dim Listener As New System.Net.Sockets.TcpListener(ipAddress, 10002)
Listener.Start()
Dim tcpClient As tcpClient = Listener.AcceptTcpClient()

but it just sits there and doesn't see any connections coming in.

On the sender side it is sending a short text message on port 10002 to host
192.168.0.2, which is my PC. Is this the same as 127.0.0.0 as defined in the
line:

Dim ipAddress As ipAddress = Dns.Resolve("localhost").AddressList(0)

above?

Thanks

-Jerry

Try replacing "localhost" with a call to Dns.GetHostName (). By using
localhost, you are binding to the loopback interface (127.0.0.1) -
which, means most likely you aren't going to receive traffic for
192.168.0.2.
 
J

Jerry Spence1

Thanks you for both replies. Steven, the two listensers was a cut and paste
problem (just to confuse!). Yes, the Dns.GetHostName () worked a treat.

Thanks very much.

-Jerry
 

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