简体   繁体   中英

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.

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 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.

Following change may help in serializing structure(s) into array:

  1. Instead of "char" array, use "unsigned char" array

  2. Use #pragma pack() for your structure definition in header file as follows:

-

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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