3
votes

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!

5

5 Answers

6
votes

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

9
votes

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.

3
votes

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

1
votes

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

1
votes

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.