尝试连接到服务器的SocketException“连接尝试失败,因为一段时间后连接方未正确响应”

时间:2019-05-28 17:13:39

标签: c# sockets tcp server client

我有一个服务器,它只是一个在计算机端口上侦听的程序。我有一个客户端程序,连接到此端口,发送请求并获取响应。为此,我使用System.Net.Socket类和tcp协议。

问题在于,当服务器位于同一台计算机上时,我的客户端可以连接,但是当我将该客户端提供给其他人时,它不起作用。 SocketException:连接尝试失败,因为一段时间后连接方未正确响应,或者连接建立失败,因为连接的主机未能响应。 我不明白,但是在我刚开始的时候,我认为有些东西对我隐藏了……

这是我的代码:

客户:

private void Connect_Click(object sender, EventArgs e)
        {
            try
            {
                sock.Connect(new IPEndPoint(IPAddress.Parse(ip.Text), 1234));
                new Thread(() =>
                {
                    read();
                }).Start();
            }
            catch (Exception ex)
            {
                //Manage exception
            }
        }

private void read()
        {
            while (true)
            {
                try
                {

                    byte[] buffer = new byte[255];
                    int rec = sock.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                    if (rec <= 0)
                        throw new SocketException();

                    Array.Resize(ref buffer, rec);

                    //Displaying received data
                    Invoke((MethodInvoker)delegate
                    {
                        requests.Items.Add(Encoding.Default.GetString(buffer));
                    });
                }
                catch
                {
                    MessageBox.Show("DISCONNECTED");
                    sock.Close();
                    break;
                }
            }

            Application.Exit();
        }

服务器:

private void Listen()
        {
            server.Listen(0);

            new Thread(AcceptOneConnection).Start();
        }

private void AcceptOneConnection()
        {
            if (client != null)
                client.Close();

            client = server.Accept();

            MessageBox.Show("Client accepted");

            while (true)
            {
                string received = ReceiveData();

                if (received == "stop connection")
                    break;

                string[] request = received.Split(' ');

                switch (request[0])
                {
                    case "register":
                        Register(request);
                        break;
                    case "login":
                        Login(request);
                        break;
                    case "disconnect":
                        UsersManager.Disconnect();
                        SendData("Disconnected");
                        break;
                    default:
                        SendData("Unknown request");
                        break;
                }
            }

            client.Close();
        }

private string ReceiveData()
        {
            try
            {
                byte[] buffer = new byte[255];
                int rec = client.Receive(buffer, 0, buffer.Length, 0);

                if (rec <= 0)
                    throw new SocketException();

                Array.Resize(ref buffer, rec);

                return Encoding.ASCII.GetString(buffer);
            }
            catch
            {
                return "";
            }
        }

我该怎么做才能使拥有客户端的人能够连接到服务器?

0 个答案:

没有答案