views:

178

answers:

2

I am running a client/server application. I use a textbox to let the user type in the IP address and port. I try to connect to the server, using 127.0.0.1 and there is no problem. After that I tried using 192.168.2.102 (NAT ip address of this computer), and it fails. Any idea why?

the code I am using is: (this the the part that connects)

connect(string IPaddress, int port)
{
    TcpCLient connection = new TcpClient();
    connection.Connect(IPaddress, port);
}

I checked with debug, it DOES use the right IPaddress and port. Firewall should allow it to connect. It's weird.

EDIT: I think I know the problem. At the server side, I use

_listener = new TcpListener(IPAddress.Loopback, 8001);

And I think that's the reason why it only accepts connections from 127.0.0.1. But then, what should I use instead? I just want any connection from any IP with this port.

A: 

First of all, the only circumstance I've ever seen return Connection was not possible because the destination computer actively rejected it is when there is actually nothing listening for connections at the specified address and port... or there is a firewall.

You may want to verify with a tracert that 192.168.2.102 actually points where you think it does. Another option is to use telnet from the command line to connect to the address and port if you really suspect there is a problem in your code (although I cannot see that apply here).

Addendum:
Is there any other application that can successfully make a connection to that specific IP address on that same machine?

jerryjvl
+3  A: 

If you are specifying IPAddress.Loopback, then only connections to 127.0.0.1 will work. Replace it with IPAddress.Any to tell your server to listen on all interfaces.

adrianbanks
yes, just saw it myself. Thanks
Nefzen