PDA

View Full Version : QTcpSocket and QDataStream



December
22nd March 2008, 13:07
I'm learning a bit about TCP and QT by writing a server and client.

I have noticed that the examples (Fortune Cookie server and client) use QDataStream to send and receive, and place a quint16 before the message indicating the expected size.

I am curious why this approach was used. I have not seen any other servers I have looked at do anything similar (they seem to simply send the data without the quint16 at the start).

I've been able to make it work both ways (with and without) but wonder which I should use?

Is there some QT specific reason to use the quint16 method?

Thanks in advance :)

wysota
22nd March 2008, 15:13
I am curious why this approach was used.
For simplicity.


I have not seen any other servers I have looked at do anything similar (they seem to simply send the data without the quint16 at the start).
Have you seen any xml-rpc based ones? Especially ones that use SOAP? Or ASN.1?

As for other servers, they mostly use a text based protocol that separates messages with newline characters (like HTTP does for example)


I've been able to make it work both ways (with and without) but wonder which I should use?
Whichever you see fit.


Is there some QT specific reason to use the quint16 method?

QDataStream is a general purpose serialization mechanism, not a general purpose binary stream. It takes care of things like different endianness, string sizes, etc. Don't mix the two. QDataStream enables you to simply pipe your structures to the socket and expect them to be read correctly regardless of what platform listens at the other end as long as it uses QDataStream as well. QDataStream is mostly used for other things than sending data through network, but it works that way too.