Hi, do I have to consider byte orders while working with TCPsocket in case of SDLNet_TCP_Recv and SDLNet_TCP_Send. If yes: what’s the way to handle endianness in this case?
Kind regards
Glocke
Hi, do I have to consider byte orders while working with TCPsocket in case of SDLNet_TCP_Recv and SDLNet_TCP_Send. If yes: what’s the way to handle endianness in this case?
Kind regards
Glocke
You should pack(when sending)\unpack(when recving) all your integers in any of endianness (by your choice). For example(pseudo code):
uint32_t GetBe32(const uint8_t *data)
{
uint32_t val;
val = GetBe16(data) << 16;
val |= GetBe16(&data[2]);
return val;
}
void PutBe32(uint8_t* data,uint32_t val)
{
data[0] = val >> 24;
data[1] = val >> 16;
data[2] = val >> 8;
data[3] = val;
}
…
// When you send:
uint32_t value = 123456;
PutBe32(data,value);
SDLNet_TCP_Send(sock,data,size);
…
// When you recv:
uint32_t value;
size = SDLNet_TCP_Recv(sock, data, maxlen);
if (size > 4)
value = GetBe32(data);
else
goto error;
20.03.2013, ? 17:07, Glocke ???(?):> Hi, do I have to consider byte orders while working with TCPsocket in case of SDLNet_TCP_Recv and SDLNet_TCP_Send. If yes: what’s the way to handle endianness in this case?
Kind regards
Glocke
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
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:
A couple other good rules: