Endianess, SDL and SDL_Net


I need some help with understanding how big endian / network byte order is
used in practice. For the past couple of weeks I have been trying to learn
SDL_Net and socket programming. Having written quite a few SDL applications
previously, I thought it might be fun to try to write an IRC client. SDL
main library does all of the drawing etc while SDL_Net is used for the
network traffic.

I now have a working IRC client using SDL and SDL_Net. Right now, the
software is able to join, part, set topic etc on channels and
receive/transmit sentences. All the basics.

The thing is that i would really like to have the DCC (something like Direct
Client Protocol) support as well. It is documented here:
I first wrote two small programs (server and client) just to make sure that
opening files in binary mode and sending/receiving wouldn’t cause me hazzle.
The receiving end gets the file alright and is able to write a copy to my
disk. So thats all good.

When I try to implement this into my SDL IRC program, things are more
complicated. I receive packets of 1024 bytes, just like before. The URL
posted says this: “The recipient should acknowledge each packet by
transmitting the total number of bytes received as an unsigned, 4 byte
integer in network byte order.”

I am using mIRC to DCC SEND to my own software, and mIRC seems to support a
feature known as “send-ahead” See

It basically says that although every packet is to be acknowledged by the
receiver (my program) it is OK to send a bunch of packets before it waits
for confirmation that bytes have been received. So I have a counter that
keeps track of the total amount of bytes received. If i do not send anything
back (commenting out the SDLNet_TCP_Send part) then mIRC will send 50 or so
packets before the file transmission progress bar halts. Most of them
contain 1024 bytes, but a few are different. The 16th packet contains 708
bytes and the 17th contains 316 bytes. After that, I receive 3 more
1024-byte packets and then the phenomena of the 16th and 17th happens again.
3 more 1024, and then… well you get it.

The total amount of bytes received i try to convert using SDL_Swap32 /
SDL_SwapBE32 and sending it back. If i do this, the mIRC progress bar will
go on like before for the 45 packets I am able to receive, and then it goes
to 100%. Is this indicating that my software tells mIRC that all of the
bytes has been received? Does the conversion from host byte order (Little
endian, x86) to NBO/Big Endian output values that are bigger than what mIRC
expects? And therefore believes the transmission is done? I have tried
several different endianess conversion functions with the same results.

Outputting the total amount of bytes received in format Uint32 vs NBO does
not make much sense to me. Tried reading up on the subject but…
Interestingly, just after receiving a packet with other than 1024 bytes of
data (708 or 316 bytes) the total amount of data is correct but the
converted NBO Uint32 suddenly flips and becomes negative? Do I have to wait
until the trailing 316-packet arrives before i send back the total + 1024
byes has been received? Is there any way i can make sure the sent NBO Uint32
data is correct except for swapping is twice and printf-ing it?

The file received is always 45056 bytes (44*1024 bytes) and the data is ok.
A 100KB jpg file shows half the image, an mp3 contains the first two secs
of music etc. If I send a file smaller than 45056 bytes, all is good.

Any help at all would be GREATLY appreciated, as I have been struggeling
with this for too long now. I use Dev-cpp with the mingw32 compiler (which
work super on my own server/client DCC-w/o-confirmation apps)

The code I’m messing about with:

… connecting a socket to the sending machine/ mIRC…
//fout is the output-file

int skicka_len = 0, result = 0;
Uint32 tot_count = 0, korv_count = 0;
char buf[1024];


// read the buffer from client

printf(“SDLNet_TCP_Recv: %s\n”,SDLNet_GetError());
printf(“Recv :(((\n”);
return 0;

tot_count += len /1024?!/;
korv_count = SDL_Swap32(tot_count);
sprintf(buf, “%d”, korv_count);
printf("Recv:%i (%i) bytes of data. ",len, tot_count);

len = strlen(buf);

if(result<len) {
if(SDLNet_GetError() && strlen(SDLNet_GetError()))
printf(“SDL_IRC SDLNet_TCP_Send: %s\n”, SDLNet_GetError());

    //printf("tot:%i, %s, %i\n", tot_count, data, len);
    for( i = 0; i < len; i++ )
    if (len) 
       goto nyttmeddelande;
fout.close();   //close the output-file


mIRC is receiving numbers from my program, but they are incorrect and makes
mIRC DCC progress bar go to 100% but still says “Transfer incomplete” I get
45 056 bytes of data… Please help! I’ve tried tons :frowning: Tell me if you need
more information.
View this message in context: http://www.nabble.com/Endianess%2C-SDL-and-SDL_Net-tf1951790.html#a5352436
Sent from the SDL forum at Nabble.com.