PDA

View Full Version : help with readyRead() signal "mechanics"



high_flyer
13th August 2006, 15:45
Hi,

In the new "C++ programming with Qt4" book, in page 331, there is an example of a server-client application, and in the method that does the actual data reading from the server there is the following code:


//'in' is a QDataStream bound to the socket
if(batesAvailable() < sizeof(quint16) ) //the first fielld in the block should be sizeof quint16
return;
in>> nextBlockSize; //read the first field which holds the next block size;
if(bytesAvailable() < nextBlockSize)
return;

//code for reading the whole block.

What I understand from this code is, that the readyRead() signal is emmited each time a new byte is added to the send buffer on the server side, other wise the code above would not work (and I am assuming it does).
The text in the book doesn't relate at all to this point, so I was wondering if some of you with some experience with socket programming with Qt4 could confirm my assumption.
The reason I have trouble accepting this, is that it means that QTcpSocket class will emit as many signals as bytes that the server will send, and seems to me that its a lot of overhead....
On the other hand it could be I am missing here something, and hope you can point me to what it might be :)

Thanks.

danadam
13th August 2006, 17:57
Hello!

I admit that I'm not a qt expert but I guess that signal is emitted every time a TCP segment is received. And TCP stack at client side decides how many bytes will be in that segment. So if client wants to send ie. 100 bytes of data, that it can be received at server side all in one segment (and one signal will be emitted) or in 2 segments (and two signals will be emitted) or in 3 segments, etc..

high_flyer
13th August 2006, 18:15
Thats sounds logical, thanks.
With your comment, the docs make more sense to me now:


void QIODevice::readyRead () [signal]

This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.

readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true).

See also bytesWritten().