views:

703

answers:

5

Hello,

I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas...

Here are the relevant sections of code:

server:

Console.Out.WriteLine("About to bind address");
IPAddress ipAd = IPAddress.Parse("127.0.0.1"); 
Console.Out.WriteLine("Choose a port to bind...");

String port = Console.In.ReadLine();
int iPort = Int32.Parse(port);

TcpListener myList = new TcpListener(ipAd, iPort);

myList.Start();

Console.WriteLine("The server is running at: "+myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");

Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

client:

Console.Out.WriteLine("enter address: ");
string address = Console.In.ReadLine();
Console.Out.WriteLine("enter port: ");
int port = Convert.ToInt32(Console.In.ReadLine());

TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");

Console.Out.WriteLine("Address: " + address + ":" + port);
tcpclnt.Connect(address, port);

I am able to ping the server from the client machine, however I am unable to telnet to the server using the bound port. I've tried a variety of ports (a few in the low 8000s and a few up around 40000). I have disable windows firewall on both systems. The systems are connected to a router which is not on the internet. I've tried with and without port forwarding set to forward incoming requests on the given port to the server machine with no effect.

The only exception that I've been able to trap is thrown by the client:

No connection could be made because the target machine actively refuses it.

I checked for an InnerException but it seems that there are none - that is the base exception. Could that be right?

Not sure what else I should be looking at - any additional troubleshooting steps would be helpful.

Thanks!

+1  A: 

try netstat -al on your machine (the exact command line varies between Windows and unix) and see if the server is listening on the port

Nick Fortescue
good thought - have tried this and it claims that the expected port is bound and listening!
sweeney
+1  A: 

The code above is listening to request coming from the loopback address. This will effectively only listen to connection on that network, and that network only includes your machine.

Have you tried listening to the address bound to the network from which the connection should be coming? On a local network it should be something like 192.168.x.x or 10.x.x.x

Coincoin
i should have just asked you in the first place - that did the trick. any idea why it makes a difference?
sweeney
Your machine is sitting on several IP networks at the same time, in your case, the loopback and the local network. You explicitly asked to only listen to 127.0.0.1 and the only machine on 127.0.0.1/32 is yours. The machine could connect to itself (through 127.0.0.1/32), but no other could.
Coincoin
+1  A: 

Why don't you use .NET remoting? It is better than doing a TCP/IP client server. You can pass messages between objects.

David Basarab
were i doing something complicated and worthy i think i'd take your advice, however the software i'm writing atm *needs* to be painfully simple in order to be appropriate for us. additionally i have no need to pass objects back and forth. i'll keep it in mind tho!
sweeney
+1  A: 

Have you tried running the client server on the same machine to make sure the connection is made first? Beyond that try using the router assigned or static IP of the machine running the server vs binding to loopback.

Quintin Robinson
+4  A: 

I've run into this before. The trick is to bind to 0.0.0.0 rather than 127.0.0.1. When you bind to 127.0.0.1 the server will only accept connections from localhost. Binding to 0.0.0.0 it will accept all requests.

You also may want to nmap the host machine from the client and see what ports it sees as being open.

EDIT: If you hard code the IP address of the machine in it the listener will only listen on that network interface. If you use 0.0.0.0 the listener will listen on all available network interfaces. This includes interfaces between your computer and a USB attached hand held device, a second network card or a VPN link.

Mykroft
I've run into that as well.
Quintin Robinson
so what would be the difference between using 0.0.0.0 and the actual IP address as suggested by Coincoin above? thats the solution that I went with and it seems to work just fine.
sweeney
The difference is if you hard code the IP address it will only work on a network interface with that address. If you use 0.0.0.0 it will listen on all available network interfaces.
Mykroft