大家都知道tcp会粘包的,比如你用1m去接收,它正常的单条数据是220,那么就会产生 5 到 6 个粘包体,我们只能打标识,用结束字符去分割处理。我写的一个类,仅供大家参考。
public void Split(byte[] receiveData){ //整体的标识 List<int> end_index = new List<int>(); ///循环该粘包总体数据 for (int i = 0; i < receiveData.Length; i++) { //最后4位直接检索 if (i + 3 == receiveData.Length) break; //先找到粘包的结束标识 if (receiveData[i] == 0xaa && receiveData[i+1] == 0xaa && receiveData[i+2] == 0xaa && receiveData[i+3] == 0xaa) { end_index.Add(i + 3);//结束的标识 } } for (int i = 0; i < end_index.Count; i++) { //判断是否有未结束的数据 if (redis_buffer_byte.Length != 0) { // 未结束的 + 第一个结束标识 byte[] RealData = new byte[end_index[i] + redis_buffer_byte.Length]; redis_buffer_byte.CopyTo(RealData, 0); receiveData.Skip(end_index[i]) .Take(end_index[i]+i).ToArray() .CopyTo(RealData, redis_buffer_byte.Length); //清空剩余byte redis_buffer_byte = new byte[] { }; if (RealData.Length == 108 || RealData.Length == receiveData.Length) return; TCP_AddWireless(RealData); } else { //不证明是最后一个 if (i != end_index.Count -1) { // 前一个标识 + 后一个标拾 byte[] RealData = new byte[end_index[i+1] - end_index[i]]; //receiveData.CopyTo(RealData,end_index[i]); Array.Copy(receiveData,end_index[i],RealData,0, RealData.Length); if (RealData.Length == 108) return; TCP_AddWireless(RealData); } else { //添加到剩余粘包队列中 byte[] RealData = new byte[receiveData.Length - end_index[i]]; redis_buffer_byte = RealData; } } } }
这样调试起来还是很费劲的,如果是无脑的话哈哈哈,你用这个吧。
public void Split_TcpData(byte[] receiveData) { for (int i = 0; i < receiveData.Length; i++) { if (i + 3 == receiveData.Length) break; if (receiveData[i] == 0xee && receiveData[i + 1] == 0xee && receiveData[i + 2] == 0xee && receiveData[i + 3] == 0xee) { for (int y = i; y < receiveData.Length; y++) { if (y + 3 == receiveData.Length) break; if (receiveData[y] == 0xaa && receiveData[y + 1] == 0xaa && receiveData[y + 2] == 0xaa && receiveData[y+ 3] == 0xaa) { byte[] RealData = new byte[y - i + 4 ]; Array.Copy(receiveData, i, RealData, 0,RealData.Length); if (RealData.Length > 146 && RealData.Length < 184) { TCP_AddWireless(RealData); } } } } } }
都是通过结束标识来搞的