PDA

View Full Version : QDataStream extra bytes in output?



grellsworth
8th November 2007, 21:21
I have created a small TCP server application and attached it to this message.

Notice in server_thread.cpp, there are two "BOOKMARK" comments.

The user connects and types in some text, the server is supposed to echo it back to them preceded by two less-than symbols and one space ("<< ").

Notice that the number of bytes to write is ALWAYS four bytes more than what was read from the user (there's a print statement near both BOOKMARKs). Where are these four extra bytes coming from?

I've even tried taking out the "<< ", but that's not where the extra bytes are coming from.

Any suggestions would be appreciated.

Sincerely,

Gordon E.

wysota
8th November 2007, 21:23
QDataStream is a serialization mechanism, not a general binary stream - it adds some additional data that allows deserialization of the stream later on. In your case the additional four bytes probably contain the length of the string you redirect into the stream.

jacek
8th November 2007, 21:23
Where are these four extra bytes coming from?
It's the version number.

http://doc.trolltech.com/4.3/qdatastream.html#versioning
http://doc.trolltech.com/4.3/datastreamformat.html

grellsworth
14th November 2007, 17:14
I've ruled out the extra bytes being the version, as they change depending on how much the user types in. So I'm thinking that that it probably is the length.

However, I've re-written my program in two parts, a server and a client... and I've followed the examples given in 'threaded fortune server' and 'fortune client', but I'm still getting extra "junk" somewhere.

When I send data from the client to the server, I send the number of bytes being sent as a serialized quint16 (as in fortune server), and I extract that number on the server side and test it against bytesAvailable() (as in fortune client). Then when the server sends back to the client, I follow the same step, I send the number of bytes to be sent as a serialized quint16.

Somehow I'm still getting "junk" somewhere.

I've attached my server and client code in .zip format.

wysota
14th November 2007, 17:50
Get rid of the data stream and write data to the socket directly using QIODevice::write.

grellsworth
15th November 2007, 14:33
OK, I've fixed it without getting rid of the data stream.

I was doing this before (for some reason this injects garbage out):

This is in the server code...

outDataStream << (quint16)0;
outDataStream << "<< ";
outDataStream << output;
outDataStream << (quint16)(outByteArray.size() - sizeof(quint16));

And now I'm doing this, instead (this works fine):

outDataStream << (quint16)0;
outDataStream << "<< " + output;
outDataStream << (quint16)(outByteArray.size() - sizeof(quint16));

I'm not sure what the real difference is, but the second one works flawlessly.

Thanks for all your help.

Gordon E.

jacek
15th November 2007, 14:41
The difference is that in the first case you send two strings, not one, and each of them is serialized separately.