问题描述
假设我有一个结构体,我想使用 winsock 2 通过网络将其成员值发送到另一个系统.我使用的是 C++ 语言.我如何将它转换为 char * 记住结构必须在发送之前序列化,以及如何在另一端将 char * 反序列化为结构?我发现 boost 序列化是对类似问题的建议,但谁能用一个小代码片段来说明序列化和反序列化?
Suppose i have a struct whose member values i want to send over the network to another system using winsock 2. I'm using C++ language. How do i convert it to char * keeping in mind that the struct has to be serialized before sending and also how do i deserialize the char * into struct at the other end? I found boost serialization as a suggestion to similar question but can anyone illustrate with a small code snippet for both serialization and deserialization ?
这个问题可能看起来很基本,但相关帖子的其他答案并没有太大帮助.
This question might seem very basic but the other answers to the related posts did not help much.
推荐答案
以下示例显示了将 struct
序列化为 char
数组并对其进行反序列化的最简单方法.
Following example shows a simplest way to serialize struct
into char
array and de-serialize it.
这篇关于结构体的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!