简体   繁体   English

C如何将结构数组序列化为char数组?

[英]C how to serialize array of structs to char array?

This code serialize struct to char array, and I can send it through sockets and deserialize back. 这段代码将结构序列化为char数组,我可以通过套接字将其发送并反序列化。

How to modify this code to use array of structure 如何修改此代码以使用结构数组

server_message[n].response =  strdup("RESPONSE");
server_message[n].command = strdup("COMMAND");
server_message[n].data = strdup("DATA");

serialize to char array - 序列化为char数组-

char reply[1024];

send through socket and deserialize back? 通过套接字发送并反序列化吗?

#include <string.h>


typedef struct __attribute__((__packed__)) arch_sm
{
    char* response;
    char* command;
    char* data;
} request_struct_sm;
size_t serialize(const request_struct_sm* arch_sm, char* buf)
{
    size_t bytes = 0;
    memcpy(buf + bytes, arch_sm->response, strlen(arch_sm->response) + 1);
    bytes += strlen(arch_sm->response) + 1;
    memcpy(buf + bytes, arch_sm->command, strlen(arch_sm->command) + 1);
    bytes += strlen(arch_sm->command) + 1;
    memcpy(buf + bytes, arch_sm->data, strlen(arch_sm->data) + 1);
    bytes += strlen(arch_sm->data) + 1;
    return bytes;
}

void deserialize_server(const char* buf, request_struct_sm* arch_sm)
{
    size_t offset = 0;
    arch_sm->response = strdup(buf + offset);
    offset += strlen(buf + offset) + 1;
    arch_sm->command = strdup(buf + offset);
    offset += strlen(buf + offset) + 1;
    arch_sm->data = strdup(buf + offset);
}

int main(){
    request_struct_sm server_message;
    request_struct_sm client_message;
    server_message.response =  strdup("RESPONSE");
    server_message.command = strdup("COMMAND");
    server_message.data = strdup("DATA");

  // server_message[0].response =  strdup("RESPONSE"); //Need to be able use array of structure
  //  server_message[0].command = strdup("COMMAND");
  //  server_message[0].data = strdup("DATA");

    char reply[1024] = {0};
    size_t bufLen = serialize(&server_message, reply);
    deserialize_server(reply, &client_message);
    printf("client_message.response = %s\n"
            "client_message.command = %s\n"
            "client_message.data = %s\n",
            client_message.response, client_message.command, client_message.data);
    return 0;

}

You basically only have two choices, and both are very similar: 基本上,您只有两个选择,并且两者非常相似:

  1. Loop over all structures in the array, serialize the current structure, send to peer. 遍历数组中的所有结构,序列化当前结构,发送给同级。 To know the beginning and end of the array, either send the number of entries first, or have a special end-of-structure marker. 要知道数组的开头和结尾,请先发送条目数,或者使用特殊的结构结束标记。

  2. Starts out the same as the previous way, but instead of sending each (serialized) structure one by one, append each (serialized) structure into a very large array, large enough to fit all of your structures. 开始时与以前的方法相同,但不是将每个(序列化的)结构一个接一个地发送,而是将每个(序列化的)结构附加到一个非常大的数组中,该数组足够大以适合您的所有结构。 Then send this whole array. 然后发送整个数组。 Again, you need some way to send the length or an end-of-data marker. 同样,您需要某种方式来发送长度或数据结束标记。

Adding to the option 2 as suggested by Joachim Pileborg. 根据Joachim Pileborg的建议,添加到选项2。

Following change may help in serializing structure(s) into array: 进行以下更改可能有助于将结构序列化为数组:

  1. Instead of "char" array, use "unsigned char" array 代替“ char”数组,使用“ unsigned char”数组

  2. Use #pragma pack() for your structure definition in header file as follows: 使用#pragma pack()作为头文件中的结构定义,如下所示:

- --

#pragma pack(push, 1)
struct Example       
{           
    char ch;           
    int val;           
    short opt;           
    ...       
};   
#pragma pack(pop)

This will remove the requirement to send the structure length value as part of serialized data to peer. 这将消除将结构长度值作为序列化数据的一部分发送给对等方的要求。 Both sender and receiver will have the header file and the same length (using sizeof()) for the structure. 发送者和接收者都将具有该结构的头文件和相同的长度(使用sizeof())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM