C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全
服务器端:
TcpListener listener = new TcpListener(IP, 1234); listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();//接受一个Client
byte[] buffer = new byte[client.ReceiveBufferSize];
NetworkStream stream = client.GetStream();//获取网络流
stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据
stream.Close();//关闭流
client.Close();//关闭Client
receiveString = Encoding.Default.GetString(buffer).Trim(' ');//转换成字符串
Console.WriteLine(receiveString);
}
listener.Stop();
客户端:
TcpClient client = new TcpClient(ip, 1234); NetworkStream sendStream = client.GetStream(); Byte[] sendBytes = Encoding.Default.GetBytes(msg); sendStream.Write(sendBytes, 0, sendBytes.Length); sendStream.Flush();
sendStream.Close();//关闭网络流
client.Close();//关闭客户端