I have to serialize some data and after send it using socket AF_UNIX. I have read (here) that I can't create a struct and just send it using a cast (void *). So, I would like to know which is the best method to handle this problem. Thank you!
Asked
Active
Viewed 434 times
0
-
1Check this question of mine: https://stackoverflow.com/questions/30945121/dealing-with-data-serialization-without-violating-the-strict-aliasing-rule – Eugene Sh. May 27 '21 at 14:55
-
1As long as sending and receiving system have the same endianness, encoding, and type widths, and your struct does not contain pointers, you can simply send its contents as is. – the busybee May 27 '21 at 15:08
-
2@thebusybee And the most important - struct padding – Eugene Sh. May 27 '21 at 15:14
-
1@EugeneSh. Correct. ;-) – the busybee May 27 '21 at 15:15
1 Answers
1
You basically have two options:
- Roll your own exactly as in the question you referenced.
- Use an off-the-shelf serialization/deserialization system. If you go this route, Google's Protocol Buffers are pretty much industry standard. For constrained projects (embedded, etc.) nanopb is a good choice for the implementation.

Jon Reeves
- 2,426
- 3
- 14