1

我正在尝试使用 C# 创建服务器。目前,我有两个程序,一个服务器和一个客户端,当它们都在同一台计算机上运行时它们都可以正常工作,服务器TcpListener是使用 ip 127.0.0.1 (localhost) 创建的,客户端连接到 ip 127.0.0.1 . 但是,当我尝试使用我的公共 IP 连接到服务器时,客户端程序等待大约 20 秒,然后我得到一个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

我已经端口转发了我正在使用的端口(端口是 2345)。我在另一台计算机上尝试了与上述相同的步骤,但仍然得到相同的结果。我在创建服务器时尝试使用公共 ip,TcpListener但后来我得到了SocketException: The requested address is not valid in its context,所以我假设我应该只使用 127.0.0.1 (虽然我可能错了,这是我第一次尝试这样的事情)。

这是创建服务器的函数。

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);
        }
    }
}

以及创建客户端的功能。

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();
    }
}

我认为我目前所拥有的会起作用,但我得到的只是那个例外。

4

2 回答 2

2

问题是IPAddress ip = IPAddress.Parse(ipStr);服务器程序的行应该是IPAddress ip = IPAddress.Any;

于 2019-01-15T00:36:12.677 回答
0

这是因为您计算机的防火墙阻止了连接。在防火墙的入站规则中,为您使用的端口添加例外。

于 2019-01-13T23:43:49.187 回答