c#中tcp 通讯消息把包长定义到发送数据中 [msg.length,data]方式的实现

本文介绍了C#中TCP通信时如何在数据包中包含消息长度信息,以便于接收方正确解析数据。参考了一篇CSDN博客,详细说明了客户端和服务器端的发送及接收规则,并探讨了固定长度数据包的另一种实现方案。
摘要由CSDN通过智能技术生成

c#中tcp 通讯消息把包长定义到发送数据中 [msg.length,data]方式的实现

参考:
https://blog.csdn.net/weixin_42033401/article/details/81558391

client或者server发送的规则

 public void Send(string str)
    {       
        //2.把消息长度封装在消息中
        byte[] data = Encoding.UTF8.GetBytes(str);
        byte[] head = BitConverter.GetBytes(data.Length+4);
        byte[] msg = head.Concat(data).ToArray();
        socket.Send(msg);
    }

client或者server接收的规则

//用来存储接收的缓存
    private byte[] ReceiveBuf = new byte[1024];
 //提前声明的缓存区
 byte[] surplusBuffer = null;
 public void ReceiveMeg(int len, byte[] recBuffer)
    {
        byte[] data;
        int StartIndex = 0;

        if (surplusBuffer == null)
        {
            data = new byte[len];
            Array.Copy(recBuffer, data, len);
        }
        else
        {
            data = surplusBuffer.Concat(recBuffer).ToArray();
            len += surplusBuffer.Length;
           surplusBuffer = null;
           
        }



        while (true)
        {

            //如果接受到的消息不为0(不为空)

            int HeadLength = 0;//包头长度(包头+包体)
            if (data.Length - StartIndex < 4)//包头接受不完整
            {
                HeadLength = -1;
            }
            else
            {
                //如果包头接受完整  转换成int类型的数值
                HeadLength = BitConverter.ToInt32(data, StartIndex);
            }
            //包头接受完整但是消息体不完整              //包头接受不完整
            //↓↓↓↓↓↓↓↓                            ↓↓↓
            if (data.Length - StartIndex < HeadLength || HeadLength == -1)
            {
                surplusBuffer = new byte[data.Length - StartIndex];
                Array.Copy(data, StartIndex, surplusBuffer, 0, data.Length - StartIndex);
                break;
            }
            else
            {
                //如果消息(包头+包体)接受完整了,解析数据
                string str = Encoding.UTF8.GetString(data, StartIndex + 4, HeadLength - 4);
                Debug.Log("接收到客户端的消息是-----" + str);
                StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
                if (StartIndex >= len)
                {
                    break;
                }
            }
        }


    }

2.再补充一个定长的实现方案

client 
发送消息
 public void Send(string str)
    {
        byte[] data = Encoding.UTF8.GetBytes(str);
 		data.CopyTo(ReceiveBuf, 0);
 		socket.Send(ReceiveBuf);
    }
server
接收消息
 string str = Encoding.UTF8.GetString(ReceiveBuf, 0, ReceiveBuf.Length);
               // string str2 = BitConverter.ToString(ReceiveBuf, 0);
                 Array.Clear(ReceiveBuf, 0, 1024);
                 int lenth = 0;
                 for (int i = 0; i < 1024; i++)
                 {
                     if (str[i] != '\0')
                     {
                         lenth += 1;
                     }
                     else
                     {
                         break;
                     }
                 }
                 string str2 = str.Substring(0,lenth);
                 Debug.Log(str2 +"--------"+str2.Length); 
                  StartReceive();
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值