I'm trying to create a server using C#. Currently, I have two programs, a server and a client both of which work correctly when they are both run on the same computer, the server's TcpListener is created using the ip 127.0.0.1 (localhost) and the client connects to the ip 127.0.0.1. However, when I try to connect to the server using my public ip, the client program waits about 20 seconds and then I get a SocketException: 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

I have already port-forwarded the port I am using (the port is 2345). I tried the same steps as above on a different computer but I still got the same result. I tried using the public ip when creating the server's TcpListener but then I got SocketException: The requested address is not valid in its context so I'm assuming that I should just use 127.0.0.1 (I could be wrong though, this is my first time trying something like this).

Here's the function for creating the server.

public static void RunServer()
{
    string ipStr = "";
    Console.WriteLine("Enter IP...");
    ipStr = Console.ReadLine();
    if (ipStr == "localhost")
        ipStr = "127.0.0.1";
    IPAddress ip = IPAddress.Parse(ipStr);

    int port = -1;
    Console.WriteLine("Enter port...");
    while (port == -1)
    {
        try
        {
            port = int.Parse(Console.ReadLine());
        }
        catch (FormatException)
        {
            Console.WriteLine("Please enter an integer.");
        }
    }
    Console.WriteLine("Starting server on " + ip + ":" + port);

    TcpListener tcpListener;
    try
    {
        tcpListener = new TcpListener(ip, port);
        tcpListener.Start();
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception: " + e);
        Console.ReadLine();
        return;
    }

    while (true)
    {
        try
        {
            TcpClient tcpClient = tcpListener.AcceptTcpClient();

            NetworkStream networkStream = tcpClient.GetStream();

            StreamReader sr = new StreamReader(networkStream);
            string msg = sr.ReadToEnd();

            Console.WriteLine("Received message: \"" + msg + "\"");

            sr.Close();
            networkStream.Close();
            tcpClient.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e);
        }
    }
}

And the function for creating the client.

public static void RunClient()
{
    string ip = "";
    Console.WriteLine("Enter IP...");
    ip = Console.ReadLine();

    int port = -1;
    Console.WriteLine("Enter port...");
    while (port == -1)
    {
        try
        {
            port = int.Parse(Console.ReadLine());
        }
        catch (FormatException)
        {
            Console.WriteLine("Please enter an integer.");
        }
    }

    while (true)
    {
        Console.WriteLine("Enter message...");
        string msg = Console.ReadLine();
        Console.WriteLine("Sending message: \"" + msg + "\"");

        TcpClient tcpClient;
        try
        {
            tcpClient = new TcpClient(ip, port);
        }
        catch (SocketException e)
        {
            Console.WriteLine("Could not connect to server. Connection refused.");
            Console.WriteLine("Exception: " + e);
            continue;
        }

        NetworkStream networkStream = tcpClient.GetStream();

        networkStream.Write(Encoding.ASCII.GetBytes(msg), 0, msg.Length);
        Console.WriteLine("Sent message!");

        networkStream.Close();
        tcpClient.Close();
    }
}

I thought that what I have currently would work, but all I get is that exception.

2

There are 2 best solutions below

1
TheTVGuy On BEST ANSWER

The problem was that the line IPAddress ip = IPAddress.Parse(ipStr); of the server program should have been IPAddress ip = IPAddress.Any;

1
Alphonse Kurian On

This is because your computer's firewall is preventing the connection. In your firewall's inbound rule add exception for the port you are using.