发送Buffer的接收有一致,如: //发送: byte[] sendInfo = new byte[1024]; sendInfo = System.Text.UnicodeEncoding.Default.GetBytes(Message.ToCharArray()); i = socket.Send(sendInfo,sendInfo.Length ,0); //接收 byte[] serverInfo = new byte[1024]; int i = socket.Receive(serverInfo,serverInfo.Length ,SocketFlags.None); string infoTo = System.Text.UnicodeEncoding.Default.GetString(serverInfo);
TCP is stream based transmission protocol. It doesn't have the concept of "packet". All data are guaranteed to arrive destination in order, but not always orgnized into the same data trunk. On the other hand, UDP always guarantees that packets arrive in its full size and one packet at a time. So, maybe you should use UDP instead of TCP.
you can use udpclient; UdpClient receivingUdpClient = new UdpClient(); Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
//发送:
byte[] sendInfo = new byte[1024];
sendInfo = System.Text.UnicodeEncoding.Default.GetBytes(Message.ToCharArray());
i = socket.Send(sendInfo,sendInfo.Length ,0);
//接收
byte[] serverInfo = new byte[1024];
int i = socket.Receive(serverInfo,serverInfo.Length ,SocketFlags.None);
string infoTo = System.Text.UnicodeEncoding.Default.GetString(serverInfo);
On the other hand, UDP always guarantees that packets arrive in its full size and one packet at a time. So, maybe you should use UDP instead of TCP.
UdpClient receivingUdpClient = new UdpClient();
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);