views:

316

answers:

4

Hello

I'm writing a program that uses QUdpSocket for transmiting data over the network. This is my first socket program, and I've come across an interesting problem called Endianness.

My actual question in, do I have to worry about Endianness when I'm using QNetwork as my sockets library? If I do have to worry, what do I have to do to properly avoid Endianness problems?

Thanks in advance.

+2  A: 

If you are transferring binary data between two machines with a different endianess you may have to worry.

The network socket will just ship the data unchanged. If the other machines assumes that the bytes it was sent are in a particular order you have to manage that.

If you are transferring data in a known format, like an image then image format generally has something in the header to show what order it was written in and the reader/writer library will handle it. If you are inventing your own binary format - then it's upto you. You may also have to consider size, how many bytes is an int on the other machine?

The good news, most machines are Intel and for most applications shipping smaller amounts of data in ascii format will work.

Martin Beckett
+3  A: 

Generally, you need to worry about endianness (byte-order) when you transfer integers larger than a single byte from one computer to another. In C/C++, this means that if you're sending something like a 16-bit, 32-bit or 64-bit integer, you need to first convert the integer to network byte order, (also known as Big-Endian). The computer on the receiving end must then convert the incoming integers to host byte order, which is whatever byte-order the host-machine uses natively. This is usually done using the htons and ntohs series of library functions, but with the Qt library you can also use the qToBigEndian and qFromBigEndian functions.

Note that you don't need to worry about endianness when sending ASCII or UTF-8 text, because these formats are composed of sequences of individual bytes, rather than multi-byte data types.

Charles Salvia
It's not always necessary or sensible to do this, if you were streaming video it wouldn't make sense to convert every byte to htons and then back again - it would be better to have a code so the receiving machine knew which way round the data was.
Martin Beckett
A: 

Intel CPUs use little endian. Most everything else is big endian. The default network byte order is big endian.

To test if endianness is a problem, send the value 0x12345678 and check if it makes it as the same or 0x78563412. If the value on the receiving computer is the later value, then the endianness is not the same between the two systems.

Check this wikipedia article for info on endianness.

zooropa
Intel CPUS use little endian - it's not a function of the OS.
Martin Beckett
It's misleading to say Windows uses little endian, and "everything else" uses Big Endian. Endianness is a property of processor architecture, not OS. Most home computers are little endian because Intel and AMD x86 processors are little endian. This includes x86 machines running Linux or other OS besides Windows.
Charles Salvia
Yes Intel macs are little endian, PowerPC were big endian (the chip is switchable but Mac used native mode), ARM (iStuff) is implementation defined
Martin Beckett
A: 

Here's a simple test for big endianness, in case you want it:

// This test assumes an int size of at least 2.

static const int testValue = 1;

#define is_bigendian() ( ((char)&testValue) == 0 )

bool CD_is_bigendian(void) { return is_bigendian(); }

Bruce