序列化结构 [英] Serialization of struct

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

问题描述

假设我有一个结构体的成员值,我想通过网络发送到另一个系统使用winsock 2.我使用C ++语言。
如何将它转换为char *记住结构必须在发送之前序列化,以及我如何反序列化char *到另一端的结构?我发现boost序列化作为类似问题的建议,但任何人都可以用一个小的代码片段用于序列化和反序列化?



这个问题可能看起来很基本,但其他答案

下面的例子显示了一个最简单的方法来序列化 struct

/ code>转换为 char 数组并反序列化。

  #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\0);
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 {
* 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 {
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;
}


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.

解决方案

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\0");
    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;
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆