Home All Groups Group Topic Archive Search About

TCPClient and TCPListener over the network

Author
30 Mar 2005 7:32 PM
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

Author
30 Mar 2005 8:01 PM
Tom Shelton
In article <OIsW38VNFHA.3***@TK2MSFTNGP14.phx.gbl>, Dave Coate wrote:
> 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
--
Tom Shelton [MVP]
Author
30 Mar 2005 8:34 PM
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

Show quoteHide quote
"Tom Shelton" <t**@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uNSJSNWNFHA.1268@TK2MSFTNGP14.phx.gbl...
> In article <OIsW38VNFHA.3***@TK2MSFTNGP14.phx.gbl>, Dave Coate wrote:
> > 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
> --
> Tom Shelton [MVP]