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!
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.