使用 UDP 协议从 Windows 套接字发送到 Qt 套接字的网络数据包上的结构编码和解码
Structure encoding and decoding on the network packet sent from the Windows Socket to Qt Socket using the UDP Protocol
如何对从 C++ 套接字发送到 Qt UDP 套接字的结构进行序列化和反序列化
实际上我正在尝试这种逻辑进行序列化和去分离化喜欢这个
struct stCardInfo
{
unsigned short nsCardType;
unsigned short nsNoOfSignals;
unsigned short nsStatus;
};
struct stErrorInfo
{
unsigned short nsErrorType;
char ErrorInfo[8];
};
struct stInitHealthCheck
{
unsigned short nsNoOfCards;
stCardInfo* lsCardInfo;
};
stInitHealthCheck stHealthCheck;
unsigned short *q = (unsigned short *)data;
stHealthCheck.nsNoOfCards = *q; q++;
stHealthCheck.lsCardInfo = new stCardInfo;
stHealthCheck.lsCardInfo->nsCardType = *q; q++;
stHealthCheck.lsCardInfo->nsNoOfSignals = *q; q++;
stHealthCheck.lsCardInfo->nsStatus= *q; q++;
但是如果结构更多,那么我每次解码到特定数据类型将变得复杂且耗时所以我问是否有任何有效的方法来进行序列化和反序列化,以便我可以非常快速地解码和编码发送和接收的UDP数据包
通信设备上的字节序可能不同,在发送/接收unsigned short
时应使用 htons
或 ntohs
。
此外,如果要将结构转换为发送端的char*
缓冲区,请注意,这不是编写通信协议的跨平台方式。使用不同的编译器编译代码时,结构在二进制级别可能具有不同的表示形式。
但是,当您有许多已经可用的序列化库时,为什么要弄乱所有这些低级的东西。只需从列表中选择一个!
如果双方都使用Qt,则可以使用QDataStream
。您可以查看Qt财富客户端/服务器进行演示。
如果其中一个不使用Qt,你可能想使用JSON(在Qt中支持,并且有许多JSON C++库(。如果您更喜欢二进制格式,则可能需要查看msgpack。
相关文章:
- 通过套接字发送的自定义数据包
- 使用 UDP 协议从 Windows 套接字发送到 Qt 套接字的网络数据包上的结构编码和解码
- C++ - 构造带有标头的数据包并通过 UDP 套接字发送
- SFML 套接字 发送数据包时设置为 sf::套接字::错误
- 为什么TCP套接字在接收到不正确的ACK时发送RST数据包而不是重新发送数据
- 使用c++套接字编程的最大TCP数据包大小
- 哪些 IO 操作会导致在使用套接字时将完成数据包发送到完成端口
- 套接字接收流在接收 FIN 数据包时是否会关闭
- 如何在使用WSARecv()和IOCP时知道套接字何时接收到FIN数据包
- 使用SFML TCP套接字和数据包破坏文件
- 在 Linux 原始套接字中接收完全修改的 icmp 数据包
- 用于数据包注入的 Libnet 与原始套接字
- 具有多个逻辑数据流的单个套接字连接(区分数据包)
- 通过原始套接字接收"syn"数据包时,如何使用"syn ack"数据包进行响应?
- 如何在WinSock2中的DGRAM套接字上获取传入数据包的IP地址
- 关于数据包和缓冲区大小的TCP套接字编程
- c++通过套接字在一个数据包中发送大数据
- 向给定remote_endpoint对象和套接字的客户端发送数据包
- c++ Unix域套接字工具包
- c++ UDP套接字在一定频率上损坏数据包