PDA

View Full Version : Fortune Server example



babu198649
20th December 2007, 15:43
hi
in the fortune server example i need a explanation for the below statement

out << (quint16)(block.size() - sizeof(quint16));

which is inside the void Server::sendFortune() function.

which can be found at the following url
http://doc.trolltech.com/4.3/network-fortuneserver-server-cpp.html

while we need the size of the block to be specified at the first two bytes
why the size of the block is subtracted with the 2bytes(i.e quint16):confused: .

bye:)

jpn
20th December 2007, 16:23
Don't double post, please.

It's well explained in the example:




out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

At the start of our QByteArray, we reserve space for a 16 bit integer that will contain the total size of the data block we are sending. We continue by streaming in a random fortune. Then we seek back to the beginning of the QByteArray, and overwrite the reserved 16 bit integer value with the total size of the array. By doing this, we provide a way for clients to verify how much data they can expect before reading the whole packet.

In other words, it's the size of the fortune, which is the size of the whole block minus the size of the quint16 in the beginning.

fareenafatima
19th February 2011, 09:04
hello,
when i run the fortune server , it says me to run the fortune client and when i run fortune client alongside, and i enter the port no and click get fortune button, it doenot proceed any further and continues to say now run fortune server example alongside