0
votes

Here is a small block of code above a read() statement in a synchronous TCP client that I've written.

    std::cout << "available? " << socket->bytesAvailable() << std::endl;
    socket->waitForReadyRead();
    std::cout << "reading..." << std::endl;
    bytesRead = socket->read(message + totalBytesRead, messageSize - totalBytesRead);

The following line:

socket->bytesAvailable()

returns 4, so there is obviously data available to be read. The problem that I'm having is that waitForReadyRead() is blocking until the default timeout of 30 seconds. Read() then proceeds to read 0 bytes on the following line.

So if there are bytes available to be read, why does waitForReadyRead() block

1

1 Answers

0
votes

from QIODevice::waitForReadyRead documentation:

Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out.

Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).

The best why to handle network connection is to use signal/slots mechanism (the asynchronous why)