我们知道TCP是基于数据流的一种协议,所以在实际中可能会遇到粘包和半包问题
粘包:因send方法是将数据写到缓冲区,后由操作系统发送数据,所以可能会遇到这种情况,假设我们要发送一个hello和world(两个send分别发),但是可能会调用receive的时候缓冲区已经有hello和world在一起了,这样我们接收到的数据就变成了helloworld。
半包:而半包恰恰相反假如我们发送hello,到对方变成he和llo。
解决方案:
长度信息法:即在消息前面加上消息的长度用于解析(注意这个长度所占的字节数需要固定,我这里用两个字节),比如发送hello,我们先求一下长度是05那我们发送过去变成了05hello,对方只咬解析就可以了
下面我们来看封装数据的代码:
public void Send(string sendStr)
{
byte[] body=Encoding.UTF8.GetBytes(sendStr);
short len = (short)body.Length;
byte[] lenByte=BitConverter.GetBytes(len);
byte[] sendBytes=lenByte.Concat(body).ToArray();
}
解析数据:
static string receiveStr = "";
static byte[] readBuffer=new byte[1024];
static int bufferCount = 0;
public static void ReceiveCallback(IAsyncResult ar)
{
Socket socket = ar.AsyncState as Socket;
int count =socket.EndReceive(ar);
bufferCount += count;
OnReceiveData();
}
public static void OnReceiveData()
{
if (bufferCount <= 2)
return;
//长度
short bodyLength = BitConverter.ToInt16(readBuffer, 0);
//消息体
if (bufferCount < 2 + bodyLength)
return;
receiveStr = Encoding.UTF8.GetString(readBuffer, 2, bodyLength);
int start = 2 + bodyLength;
Array.Copy(readBuffer, start, readBuffer, 0, bufferCount - start);
bufferCount -= start;
OnReceiveData();
}