<legend id='QAImr'><style id='QAImr'><dir id='QAImr'><q id='QAImr'></q></dir></style></legend>

<small id='QAImr'></small><noframes id='QAImr'>

<i id='QAImr'><tr id='QAImr'><dt id='QAImr'><q id='QAImr'><span id='QAImr'><b id='QAImr'><form id='QAImr'><ins id='QAImr'></ins><ul id='QAImr'></ul><sub id='QAImr'></sub></form><legend id='QAImr'></legend><bdo id='QAImr'><pre id='QAImr'><center id='QAImr'></center></pre></bdo></b><th id='QAImr'></th></span></q></dt></tr></i><div id='QAImr'><tfoot id='QAImr'></tfoot><dl id='QAImr'><fieldset id='QAImr'></fieldset></dl></div>
        <bdo id='QAImr'></bdo><ul id='QAImr'></ul>
    1. <tfoot id='QAImr'></tfoot>

      结构体的序列化

      Serialization of struct(结构体的序列化)

        <legend id='Vlas5'><style id='Vlas5'><dir id='Vlas5'><q id='Vlas5'></q></dir></style></legend>

          <tbody id='Vlas5'></tbody>

        <tfoot id='Vlas5'></tfoot>
      1. <small id='Vlas5'></small><noframes id='Vlas5'>

            <i id='Vlas5'><tr id='Vlas5'><dt id='Vlas5'><q id='Vlas5'><span id='Vlas5'><b id='Vlas5'><form id='Vlas5'><ins id='Vlas5'></ins><ul id='Vlas5'></ul><sub id='Vlas5'></sub></form><legend id='Vlas5'></legend><bdo id='Vlas5'><pre id='Vlas5'><center id='Vlas5'></center></pre></bdo></b><th id='Vlas5'></th></span></q></dt></tr></i><div id='Vlas5'><tfoot id='Vlas5'></tfoot><dl id='Vlas5'><fieldset id='Vlas5'></fieldset></dl></div>
                <bdo id='Vlas5'></bdo><ul id='Vlas5'></ul>

                本文介绍了结构体的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我有一个结构体,我想使用 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.

                #include <iostream>
                #include <cstring>
                
                #define BUFSIZE 512
                #define PACKETSIZE sizeof(MSG)
                
                using namespace std;
                
                typedef struct MSG
                {
                    int type;
                    int priority;
                    int sender;
                    char message[BUFSIZE];
                }MSG;
                
                void serialize(MSG* msgPacket, char *data);
                void deserialize(char *data, MSG* msgPacket);
                void printMsg(MSG* msgPacket);
                
                int main()
                {
                    MSG* newMsg = new MSG;
                    newMsg->type = 1;
                    newMsg->priority = 9;
                    newMsg->sender = 2;
                    strcpy(newMsg->message, "hello from server");
                    printMsg(newMsg);
                
                    char data[PACKETSIZE];
                
                    serialize(newMsg, data);
                
                    MSG* temp = new MSG;
                    deserialize(data, temp);
                    printMsg(temp);
                
                    return 0;
                }
                
                void serialize(MSG* msgPacket, char *data)
                {
                    int *q = (int*)data;    
                    *q = msgPacket->type;       q++;    
                    *q = msgPacket->priority;   q++;    
                    *q = msgPacket->sender;     q++;
                
                    char *p = (char*)q;
                    int i = 0;
                    while (i < BUFSIZE)
                    {
                        *p = msgPacket->message[i];
                        p++;
                        i++;
                    }
                }
                
                void deserialize(char *data, MSG* msgPacket)
                {
                    int *q = (int*)data;    
                    msgPacket->type = *q;       q++;    
                    msgPacket->priority = *q;   q++;    
                    msgPacket->sender = *q;     q++;
                
                    char *p = (char*)q;
                    int i = 0;
                    while (i < BUFSIZE)
                    {
                        msgPacket->message[i] = *p;
                        p++;
                        i++;
                    }
                }
                
                void printMsg(MSG* msgPacket)
                {
                    cout << msgPacket->type << endl;
                    cout << msgPacket->priority << endl;
                    cout << msgPacket->sender << endl;
                    cout << msgPacket->message << endl;
                }
                

                这篇关于结构体的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                1. <tfoot id='9ASrf'></tfoot>

                      <i id='9ASrf'><tr id='9ASrf'><dt id='9ASrf'><q id='9ASrf'><span id='9ASrf'><b id='9ASrf'><form id='9ASrf'><ins id='9ASrf'></ins><ul id='9ASrf'></ul><sub id='9ASrf'></sub></form><legend id='9ASrf'></legend><bdo id='9ASrf'><pre id='9ASrf'><center id='9ASrf'></center></pre></bdo></b><th id='9ASrf'></th></span></q></dt></tr></i><div id='9ASrf'><tfoot id='9ASrf'></tfoot><dl id='9ASrf'><fieldset id='9ASrf'></fieldset></dl></div>
                        <tbody id='9ASrf'></tbody>
                        <bdo id='9ASrf'></bdo><ul id='9ASrf'></ul>

                        <small id='9ASrf'></small><noframes id='9ASrf'>

                        <legend id='9ASrf'><style id='9ASrf'><dir id='9ASrf'><q id='9ASrf'></q></dir></style></legend>