我只是注意到我的套接字客户端没有收到服务器发回给它的相同数据。文本在 8192 个字符后被截断。
有一些我不知道的限制吗?我该如何度过这个难关?
客户端使用此代码:
//Send and Receive
public string sendAndReceiveMSG(string msg)
{
try
{
NetworkStream serverStream = clientSocket.GetStream();
string sendresponse = sendMSG(msg, serverStream);
if (sendresponse == "ConnectionCrash")
{
return sendresponse;
}
else if (sendresponse == "OK")
{
string receiveresponse = receiveMSG(serverStream);
return receiveresponse;
}
return "UnknownErrorInternal";
}
catch (Exception ex)
{
return "UnknownErrorInternal";
}
}
//Send msg
private string sendMSG(string msg, NetworkStream serverStream)
{
try
{
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(msg);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
return "OK";
}
catch (Exception ex)
{
endSock();
return "ConnectionCrash";
}
}
//Receive msg
private string receiveMSG(NetworkStream serverStream)
{
try
{
byte[] inStream = new byte[10025];
int buffSize = clientSocket.ReceiveBufferSize;
dynamic bytesRead = serverStream.Read(inStream, 0, buffSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, bytesRead);
return returndata;
}
catch (Exception ex)
{
endSock();
return "ConnectionCrash";
}
}
客户端然后像这样从服务器获得响应;
string recv = client.sendAndReceiveMSG("someRequest");