TCP IP Client/Server failing to connect

-1

I have a small TCP Client server application that cannot seem to connect and is giving a timeout error and I can figure out why.

The error I am getting is

System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 161.242.249.63:5000 at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)

Below are the list of steps I have tried:

  1. tried both client and server applications on the same PC, this will work, but if the applications are on different machines than I cant seem to get a connection due to a timeout.

  2. I can ping both PC's by name and IP address. IPv6 has also been disabled. The PCs are linked by Cat5e Ethernet cable over about half a meter.

  3. I have Checked all firewall rules and allowed traffic on port 5000 on both PCs

  4. On CMD i have confirmed the port is listening using netstat -a

  5. using Putty, I can telnet from either machine and make a connection to the server

I am not sure why i cant make a connection from my client application

Server Code

//string IP = ServerSocket.getIP();
        string IP = "161.242.249.63";

        IPAddress localAdd = IPAddress.Parse(IP);
        Console.WriteLine("IP Address is :" + IP);
        TcpListener listener = new TcpListener(localAdd, 5000);
        listener.Start();
        while (true)
        {
            //---incoming client connected---
            TcpClient client = listener.AcceptTcpClient();

            //---get the incoming data through a network stream---
            NetworkStream nwStream = client.GetStream();
            byte[] buffer = new byte[client.ReceiveBufferSize];

            //---read incoming stream---
            int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

            //---convert the data received into a string---
            string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Data Received");
            string[] data = dataReceived.Split(',');
            data[2]= data[2].Remove(data[2].Length-1);
            bool validate = ValidateAgainstADAndGroup(data[0], data[1], data[2]);

            if (validate)
            {
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("*");
                //---write back the text to the client---
                Console.WriteLine("Sending back : OK " );
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                client.Close();
            }

            else
            {
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("!");
                //---write back the text to the client---
                Console.WriteLine("Sending back : Error");
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                client.Close();

            }

Client Code

 TcpClient client = new TcpClient("10.248.37.62", 5000);
        NetworkStream nwStream = client.GetStream();
        byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(message);

        //send data
        nwStream.Write(bytesToSend, 0, bytesToSend.Length);

        //---read back the text---
        byte[] bytesToRead = new byte[client.ReceiveBufferSize];
        int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
        // Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
        client.Close();

I would be greatful if anyone could help with this?

c#
tcpclient
tcpserver
asked on Stack Overflow Oct 11, 2019 by Engodel • edited Oct 11, 2019 by ADyson

1 Answer

0

This is more of a networking problem as there is nothing inherently wrong with the code provided.

It does look like youre trying to bind the server to the public IP address though which isnt correct as the system doesnt know what its own IP address is.

You should bind it to a network interface or use IPAddress.Any.

https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.any?view=netcore-3.0

answered on Stack Overflow Oct 11, 2019 by Kieran Devlin

User contributions licensed under CC BY-SA 3.0