来自客户端的连接方法在读取来自服务器的响应时卡住

Connection method from client gets stuck upon reading response from server

我有一个客户端和一个服务器应用程序。 服务器启动并在启动时像往常一样等待连接。 客户端通过我称为 SocketMessage 的自定义 Class 发送其请求,SocketMessage 仅存储客户端的用户名和包含客户端想要向服务器请求的方法的字符串。 服务端获取SocketMessage,反序列化,读取方法字符串,根据switch语句选择方法执行。

我可以从客户端发送请求,在服务器接收它并发送响应,但返回客户端,当尝试读取响应时,客户端的 Connect 方法被锁定在 streamReader.ReadLine

这是 SERVER,ClientCommunication 在子进程上 运行。

private void ClientCommunication(object cliente)
        {
            TcpClient tcCliente = (TcpClient)cliente;
            StreamReader reader = new StreamReader(tcCliente.GetStream());
            StreamWriter serverStreamWriter = new StreamWriter(tcCliente.GetStream());

            while (serverOn)
            {

                try
                {
                    
                    var msj= reader.ReadLine();
                    SocketMessage<object> msjReceived= JsonConvert.DeserializeObject<SocketMessage<object>>(msj);
                    
                    
                        SelectMethod(msjReceived.Method, msj, ref serverStreamWriter);
                    
                }
                catch (Exception ex)
                {
                   
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            tcCliente.Close();
        }

public void SelectMethod(string pMethod, string pMsj, ref StreamWriter serverStreamWriter)
        {
            switch (pMethod)
            {
                case "CONNECT":
               
                    SocketMessage<string> connectMsj = JsonConvert.DeserializeObject<SocketMessage<string>>(pMsj);  
                        LogConnection(connectMsj.Entidad);
                        serverStreamWriter.Write("CONNECTED");
                        serverStreamWriter.Flush();
                    break;
                default:
                    break;
            }
        }

这是客户端的自定义 class,它处理请求的发送。

public string RequestConnection(string pUsername)
        {
            try
            {
                
                ipServer = IPAddress.Parse("127.0.0.1");
                client = new TcpClient();
                serverEndPoint = new IPEndPoint(ipServer, 16830);
               
                client.Connect(serverEndPoint);
                string serverRsp;

                SocketMessage<string> connectionRequest= new MensajeSocket<string> { Method= "CONNECT", Entity = pUsername};
                
                clientStreamReader = new StreamReader(cliente.GetStream());
                clientStreamWriter = new StreamWriter(cliente.GetStream());
                
                clientStreamWriter.WriteLine(JsonConvert.SerializeObject(connectionRequest));
                clientStreamWriter.Flush();
                Console.WriteLine("request SENT");
                //THIS is where it gets stuck
                serverRsp = clientStreamReader.ReadLine();
                Console.WriteLine("streamReader READ " + serverRsp);
                
                return serverRsp;
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message);
                return ex.Message;
            }    
        }

为什么点击serverRsp = clientStreamReader.ReadLine();就卡住了?我知道 ReadLine 在收到响应之前会被锁定,但我正在从服务器发送响应。我已经确认服务器确实收到了请求并正确记录了它,但客户端似乎无法读取相关的“CONNECTED”消息,知道发生了什么吗?

ReadLine() 等待负载中的换行终止。由于您的客户端仅发送 Write()WriteLine() 会将换行符附加到有效负载的末尾,因此服务器/侦听器知道何时停止侦听数据。

serverStreamWriter.Write("CONNECTED");