没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
/// <summary>
/// 构建消息数据包
/// </summary>
/// <param name="Crccode">消息校验码,判断消息开始</param>
/// <param name="sessionid">用户登录成功之后获得的身份ID</param>
/// <param name="command">主命令</param>
/// <param name="subcommand">子命令</param>
/// <param name="encrypt">加密方式</param>
/// <param name="MessageBody">消息内容(string数组)</param>
/// <returns>返回构建完整的数据包</returns>
public static byte[] BuildDataPackage(int Crccode,long sessionid, int command,int subcommand, int encrypt, string[] MessageBody)
{
//消息校验码默认值为0x99FF
Crccode = 65433;
//消息头各个分类数据转换为字节数组(非字符型数据需先转换为网络序 HostToNetworkOrder:主机序转网络序)
byte[] CrccodeByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Crccode));
byte[] sessionidByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(sessionid));
byte[] commandByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(command));
byte[] subcommandByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(subcommand));
byte[] encryptByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(encrypt));
//计算消息体的长度
int MessageBodyLength = 0;
for (int i = 0; i < MessageBody.Length; i++)
{
if (MessageBody[i] == "")
break;
MessageBodyLength += Encoding.UTF8.GetBytes(MessageBody[i]).Length;
}
//定义消息体的字节数组(消息体长度MessageBodyLength + 每个消息前面有一个int变量记录该消息字节长度)
byte[] MessageBodyByte = new byte[MessageBodyLength + MessageBody.Length*4];
/// 构建消息数据包
/// </summary>
/// <param name="Crccode">消息校验码,判断消息开始</param>
/// <param name="sessionid">用户登录成功之后获得的身份ID</param>
/// <param name="command">主命令</param>
/// <param name="subcommand">子命令</param>
/// <param name="encrypt">加密方式</param>
/// <param name="MessageBody">消息内容(string数组)</param>
/// <returns>返回构建完整的数据包</returns>
public static byte[] BuildDataPackage(int Crccode,long sessionid, int command,int subcommand, int encrypt, string[] MessageBody)
{
//消息校验码默认值为0x99FF
Crccode = 65433;
//消息头各个分类数据转换为字节数组(非字符型数据需先转换为网络序 HostToNetworkOrder:主机序转网络序)
byte[] CrccodeByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Crccode));
byte[] sessionidByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(sessionid));
byte[] commandByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(command));
byte[] subcommandByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(subcommand));
byte[] encryptByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(encrypt));
//计算消息体的长度
int MessageBodyLength = 0;
for (int i = 0; i < MessageBody.Length; i++)
{
if (MessageBody[i] == "")
break;
MessageBodyLength += Encoding.UTF8.GetBytes(MessageBody[i]).Length;
}
//定义消息体的字节数组(消息体长度MessageBodyLength + 每个消息前面有一个int变量记录该消息字节长度)
byte[] MessageBodyByte = new byte[MessageBodyLength + MessageBody.Length*4];
//记录已经存入消息体数组的字节数,用于下一个消息存入时检索位置
int CopyIndex = 0;
for (int i = 0; i < MessageBody.Length; i++)
{
//单个消息
byte[] bytes = Encoding.UTF8.GetBytes(MessageBody[i]);
//先存入单个消息的长度
BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bytes.Length)).CopyTo(MessageBodyByte, CopyIndex);
CopyIndex += 4;
bytes.CopyTo(MessageBodyByte, CopyIndex);
CopyIndex += bytes.Length;
}
//定义总数据包(消息校验码4字节 + 消息长度4字节 + 身份ID8字节 + 主命令4字节 + 子命令4字节 + 加密方式4字节 + 消息体)
byte[] totalByte = new byte[28 + MessageBodyByte.Length];
//组合数据包头部(消息校验码4字节 + 消息长度4字节 + 身份ID8字节 + 主命令4字节 + 子命令4字节 + 加密方式4字节)
CrccodeByte.CopyTo(totalByte,0);
BitConverter.GetBytes(IPAddress.HostToNetworkOrder(MessageBodyByte.Length)).CopyTo(totalByte,4);
sessionidByte.CopyTo(totalByte, 8);
commandByte.CopyTo(totalByte, 16);
subcommandByte.CopyTo(totalByte, 20);
encryptByte.CopyTo(totalByte, 24);
//组合数据包体
MessageBodyByte.CopyTo(totalByte,28);
Debug.Log("发送数据包的总长度为:"+ totalByte.Length);
return totalByte;
}
///<summary>
///发送信息
///</summary>
public static void SendMessage(byte[] sendBytes)
int CopyIndex = 0;
for (int i = 0; i < MessageBody.Length; i++)
{
//单个消息
byte[] bytes = Encoding.UTF8.GetBytes(MessageBody[i]);
//先存入单个消息的长度
BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bytes.Length)).CopyTo(MessageBodyByte, CopyIndex);
CopyIndex += 4;
bytes.CopyTo(MessageBodyByte, CopyIndex);
CopyIndex += bytes.Length;
}
//定义总数据包(消息校验码4字节 + 消息长度4字节 + 身份ID8字节 + 主命令4字节 + 子命令4字节 + 加密方式4字节 + 消息体)
byte[] totalByte = new byte[28 + MessageBodyByte.Length];
//组合数据包头部(消息校验码4字节 + 消息长度4字节 + 身份ID8字节 + 主命令4字节 + 子命令4字节 + 加密方式4字节)
CrccodeByte.CopyTo(totalByte,0);
BitConverter.GetBytes(IPAddress.HostToNetworkOrder(MessageBodyByte.Length)).CopyTo(totalByte,4);
sessionidByte.CopyTo(totalByte, 8);
commandByte.CopyTo(totalByte, 16);
subcommandByte.CopyTo(totalByte, 20);
encryptByte.CopyTo(totalByte, 24);
//组合数据包体
MessageBodyByte.CopyTo(totalByte,28);
Debug.Log("发送数据包的总长度为:"+ totalByte.Length);
return totalByte;
}
///<summary>
///发送信息
///</summary>
public static void SendMessage(byte[] sendBytes)
剩余5页未读,继续阅读
资源评论
我们爱自己
- 粉丝: 0
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 筷手引流工具.apk
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功