C# TCP通信自定义包头

winform经常用到TCP通信,使用TCP通信协议的好处大家都知道,数据传送可靠,不会丢失,也就是不会丢包。但是使用TCP协议接收数据比有点麻烦,数据会粘包。为了保证数据正常的解析,这里使用最大众的解决方法,在发送的数据包前面定义一个2个字节长度的包头,根据包头可以知道后续数据的长度。具体做法是发送端发送数据时同时将数据长度写到包头,接收端默认读取两个字节,有数据来它便读到了数据包的长度,接着通过数据包的长度继续读取后续的数据。这样就可以解决数据的粘包问题了。

发送端

          string msgs="aaa";
         byte[] msg = Encoding.Default.GetBytes(msgs);           
                int length = msg.Length;
                short lengthall = (short)(length + 2);
                byte[] lengthByte =  System.BitConverter.GetBytes(lengthall);//short转字节调换位置
                byte tmp = lengthByte[0];
                lengthByte[0] = lengthByte[1];
                lengthByte[1] = tmp;
byte[] all = lengthByte.Concat(msg).ToArray(); //然后将字节数组写入网络流 if (bw != null && tcpClient.Connected==true) { bw.Write(all); bw.Flush(); } else { this.Reconnect(); }

接收端

  while (true)
            {
                byte[] BytesLenth = null;
                byte[] BytesContent = null;
                if (br == null) continue;                
                    BytesLenth = br.ReadBytes(2);
                    //字节转int调换位置
                    byte tmp = BytesLenth[0];
                    BytesLenth[0] = BytesLenth[1];
                    BytesLenth[1] = tmp;

                    int lenth = BitConverter.ToInt16(BytesLenth, 0);
                    BytesContent = br.ReadBytes(lenth - 2);
                    string conContent = Encoding.Default.GetString(BytesContent);              
            }

这里需要注意一个问题,贴的代码中int转字节或者字节转int的时候都把字节数组中的两个数据进行了对调,这个是特殊情况,在与C或者C++程序通信的时候必须这样才能正常解析,估计是不同语言读取字节数组的顺序不一样导致的。如果是C#编的客户端程序之间通信,不需要这样对调位置。

posted @ 2017-08-18 15:33  清晨时光  阅读(4253)  评论(0编辑  收藏  举报