PDA

View Full Version : QT Network htonl



csvivek
9th May 2008, 16:33
I have a data coming from a server on a TCP Connection and the data comes in network byte order.
is there any specific class in Qt that performs this conversion or should i use the native ntohs() methods of linux.

i tried using the native method call but that doesnot give any change!
can anyone please enlighten me on this....



struct{
quint16 i;
}
tcp->read((char*)i,sizeof(i));
ntohs(i);


i am working in QT4.2.2 Red Hat Linux

mitro
9th May 2008, 17:36
When working with QTcpSocket I usually use this approach:

quint16 i;
QByteArray packet;
QDataStream in(&packet, QIODevice::ReadOnly);

// This function sets the byte order of incoming data; network format is BigEndian,
// while Intel format is LittleEndian
in.setByteOrder(QDataStream::BigEndian);
packet = socket->read(sizeof(i));
in >> i;
I've learned this approach from "C++ GUI Programming with Qt 4", so I think it is the recommended one.