TCPClient and TCPListener over the network

D

Dave Coate

Hi,

I am working on a client-server app. I can get two applications to talk to
each other on the same machine using 127.0.0.1, but as soon as I try it
using a computer name or actual IP address I get a SocketException: "No
connection could be made because the target machine actively refused it".

On the server I have a listener:
Private m_Listener As TcpListener
m_Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 8000)
m_Listener.Start()

On the Client I have a client:
Dim Client As New TcpClient
Client.Connect(IPAddress.Parse("127.0.0.1"), 8000)

This runs fine and I can get the two apps to exchange information

However, if I try to use a computer name on the client to connect...
Client.Connect("ComputerName.foo.com", 8000)

I get the error I mentioned above.

Name resolution (DNS) is fine and I have administrative rights on the box.

What am I missing?

TIA
Dave
 
T

Tom Shelton

Hi,

I am working on a client-server app. I can get two applications to talk to
each other on the same machine using 127.0.0.1, but as soon as I try it
using a computer name or actual IP address I get a SocketException: "No
connection could be made because the target machine actively refused it".

On the server I have a listener:
Private m_Listener As TcpListener
m_Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 8000)
m_Listener.Start()

Your explicitly binding to the loopback interface. You need to change
your server to bind to its external ip address. I would use something
like GetHostByName...

m_Listener = New TcpListener _
(Dns.GetHostByName ("TheServersName").AddressList (0), 8000)
On the Client I have a client:
Dim Client As New TcpClient
Client.Connect(IPAddress.Parse("127.0.0.1"), 8000)

On the client use:

Client.Connect _
(Dns.GetHostByName ("TheServersName").AddressList(0), 8000)

HTH
 
D

Dave Coate

Very cool Tom! All the examples I found were using an obsolete overload of
the TcpListener constructor that did not include the binding parameter. I
could not work it out...

Your code not only works on my local computer, but between two computers as
well. Thanks!

Dave
 

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