Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CMCDragonkai/a1a50ba70b457b21d7a3e992c6045837 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/a1a50ba70b457b21d7a3e992c6045837 to your computer and use it in GitHub Desktop.
Struct Packing and Unpacking for Network Communication #c

Struct Packing and Unpacking for Network Communication

Structs in C have both host/compiler-specific padding and host endianness. When communicating over a network, the struct must be framed (prefix length or frame character), packed (this means getting rid of the padding) and ordered according to a common endianness. On the reverse, the bytestream must then be read, message framed, re-ordered, and unpacked into a struct.

It is important that the structs that are used as interfaces have fixed sizes, that is uint32_t and not int.

Prefix length message frames with big-endian ordered bytes is probably the most popular style.

We should have some C or Rust code demonstrating this procedure.

In most cases, just use protocol buffers or GRPC. Or go all the way to HTTP with just RESTful methods! https://sbdevel.wordpress.com/2009/12/17/the-case-rpc-vs-messaging/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment