cake
cake

Reputation: 1386

Qt Sockets and Endianness

I'm writing a program that uses QUdpSocket for transmitting 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?

Upvotes: 8

Views: 10040

Answers (5)

ianmac45
ianmac45

Reputation: 2268

I know this was answered a long time ago, but I just thought I'd add to it.

Instead of using the endian conversion functions, you could use the QDataStream functionality (with any QIODevice), which is much easier and more intuitive in my opinion. Of course, that would lock you into using the QDataStream on both the sender and receiver, but it's worth it for that case.

Example usage, for someone else's sample of sending an image:

QDataStream & operator<< (QDataStream& stream, const QImage& image);
QDataStream & operator>> (QDataStream& stream, QImage& image);
(Both of those are already implemented within Qt.)

QTcpSocket *tcp = ...;
QImage img = ...;
QDataStream(tcp) << img;

Upvotes: 1

Bruce
Bruce

Reputation: 2310

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(); }

Upvotes: 1

Charles Salvia
Charles Salvia

Reputation: 53289

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.

Upvotes: 13

Paul Kelly
Paul Kelly

Reputation: 4019

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.

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96109

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.

Upvotes: 3

Related Questions