Hello,
I hate to reply to my own message right off, but upon carefully examining SDL_net.h, it seems this is a glaringly obvious bug in the library. Observe:
#define SDLNet_Write32(value, areap)
do
{
Uint8 *area = (Uint8 *)(areap);
area[3] = (value>> 24) & 0xFF;
area[2] = (value>> 16) & 0xFF;
area[1] = (value>> 8) & 0xFF;
area[0] = value & 0xFF;
} while ( 0 )
This is the definition of SDLNet_Write32 used for little-endian hosts. It would be fantastic, except that it’s writing the bytes into the array in the completely wrong order. Let us say that value is 0xAABBCCDD. On a little-endian host this is stored as DDCCBBAA in consecutive memory bytes, however, bit shifts are NOT relative to host endianness – they are endian independent. This means that, for example, the byte (value>> 24) & 0xff is AA, not DD.
In order for this integer to be properly translated to big-endian, the array indices should be reversed. (value>> 24) & 0xff should be going to the first memory location, so that it ends up as AABBCCDD. This in fact makes it exactly the same as the definition of this macro for big-endian hosts. In case that seems unintuitive, let me again iterate that bit shifts are not endian dependent. This means that the only thing that matters is the relation of the shifts to the array indices, and for a fixed output format (in this case big-endian), the relationship will remain constant.
As for why this is differing in behavior between Windows and Linux, I cannot explain. There must be something different about the compilation that’s causing the macros to not be used…
James> From: @James_Haley
To: sdl at libsdl.org
Date: Wed, 17 Oct 2007 00:40:34 -0500
Subject: [SDL] Strange SDL_net problem - byte-swapped integers
Hi,
I’m adding network support to my Doom port, and everything works fine as long as the two clients are running the same OS and compiled version of the executable. But as soon as we try to mix and match, things go crazy. We’re trying to have Linux and Win32 machines talk to each other, but here’s an example of what happens:
Sent by Linux:
1982972966 01110110 00110001 11000100 00100110
Received by Windows:
650391926 00100110 11000100 00110001 01110110
As you can see, the data has been byte-swapped. This is despite the number in question having been transmitted using SDLNet_Write32, and received using SDLNet_Read32. Why is an extra byte-swap occurring to the data only when Linux is involved, and how do I fix it? AFAIK the Linux machine is little-endian so that should have nothing to do with it.
James
Help yourself to FREE treats served up daily at the Messenger Caf?. Stop by today.
http://www.cafemessenger.com/info/info_sweetstuff2.html?ocid=TXT_TAGLM_OctWLtagline
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Boo!?Scare away worms, viruses and so much more! Try Windows Live OneCare!
http://onecare.live.com/standard/en-us/purchase/trial.aspx?s_cid=wl_hotmailnews