[SDL] [SDL_net] Endianness

Nathaniel J Fries nfries88 at yahoo.com
Wed Mar 20 12:49:38 PDT 2013

Yes, SDL_net has no knowledge of the endianness of the buffers you send and receive.

Fortunately, SDL provides some excellent, optimal functions for reversing the order of bytes in SDL_endian.h

As a general rule, you have three options for transmission (either file or network) endianness:
1) Assume most machines using the transmitted data will be little-endian (a good general rule now that everyone is using x86)
2) Convert to network byte order, which is big-endian.
3) (Less practical): Have the client tell you its endianness, and convert to to client endianness before sending and to native endianness after receiving.

A couple other good rules:
1) Always transmit text as UTF-8 (if some requirement forces you to use UTF-16 or UTF-32; make sure the byte-order-mark U+FEFF is the first codepoint sent), unless you have no use of characters outside the ASCII range, and in that case transmit as ASCII.
2) Always transmit the length (in bytes or Unicode code units [what C and C++ call "characters"]) of the text before the text itself.
3) Always transmit as little data as possible (client bandwidth is often limited by their ISP, and server bandwidth isn't exactly free).
4) Never transmit floating point numbers. Convert them either to fixed precision (if the only acceptable range is between 0 and 1 or -1 and 1) or a ratio. I know SDL provides a function for byte-swapping floats, but that doesn't make it a good idea, and conversion is rather simple. And, fun fact, a 64-bit ratio can represent PI and e (Euler's number) as accurately (to the 15th decimal place) as a 64-bit double can.

Nate Fries

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130320/472bd49b/attachment-0009.htm>

More information about the SDL mailing list