catto
17th February 2011, 17:17
Hello,
I am writing an application which has to receive UDP datagrams originated from an external source. I read the datagrams from the socket using QByteArray. The datagram has several fields, a 2-byte number, a 4-byte number and a textual message. I can read the textual message with the following code:
QByteArray buffer;
// ...
// read datagram from socket
// ...
QString message = QString::fromAscii(buffer.mid(6, 10));
But the parsing of the other fields of the datagram doesn't work. For example:
bool correct = true;
quint16 cmd = buffer.mid(0, 2).toUShort(&correct);
// correct is false after conversion
I assume I can't use QDataStream to get the individual fields, as the data is not being serialized to a QDataStream on the other end of the comunication. Could anyone point out what I am doing wrong? Is there maybe a better way to parse the different fields of a datagram in order to process them on the receiving end?
Any help would be much appreciated.
Thank you,
catto
I am writing an application which has to receive UDP datagrams originated from an external source. I read the datagrams from the socket using QByteArray. The datagram has several fields, a 2-byte number, a 4-byte number and a textual message. I can read the textual message with the following code:
QByteArray buffer;
// ...
// read datagram from socket
// ...
QString message = QString::fromAscii(buffer.mid(6, 10));
But the parsing of the other fields of the datagram doesn't work. For example:
bool correct = true;
quint16 cmd = buffer.mid(0, 2).toUShort(&correct);
// correct is false after conversion
I assume I can't use QDataStream to get the individual fields, as the data is not being serialized to a QDataStream on the other end of the comunication. Could anyone point out what I am doing wrong? Is there maybe a better way to parse the different fields of a datagram in order to process them on the receiving end?
Any help would be much appreciated.
Thank you,
catto