You are essentially designing a binary network protocol, so you may want to use an existing library (like Google's protocol buffers). If you still want to design your own, you can achieve reasonable portability of writing raw structs by doing this:
- Pack your structs (GCC's
__attribute__((packed))
, MSVC's #pragma pack
). This is compiler-specific.
- Make sure your integer endianness is correct (
htons
, htonl
). This is architecture-specific.
- Do not use pointers for strings (use character buffers).
- Use C99 exact integer sizes (
uint32_t
etc).
- Ensure that the code only compiles where
CHAR_BIT
is 8, which is the most common, or otherwise handles transformation of character strings to a stream of 8-bit octets. There are some environments where CHAR_BIT
!= 8, but they tend to be special-purpose hardware.
With this you can be reasonably sure you will get the same result on the other end as long as you are using the same struct definition. I am not sure about floating point numbers representation, however, but I usually avoid sending those.
Another thing unrelated to portability you may want to address is backwards compatibility by introducing length as a first field, and/or using version tag.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…