Qt World Summit: Register Today!

QSslSocket::waitForReadyRead() not working

  • Hi,

    I'm facing very strange behaviour of QSslSocket::waitForReadyRead(int msecs).
    My code looks like this:
    while ((socket->bytesAvailable() + socket->encryptedBytesAvailable()) < size ) {
    if (!socket->waitForReadyRead(timeout)) {
    if (socket->bytesAvailable() + socket->encryptedBytesAvailable() >= size) {
    qDebug() << "HOW IS THIS POSSIBLE???";

        return QByteArray();


    It always blocks for timeout msec (3 seconds) and return false. However, new data has been received.

    I have tried connecting socket readyRead() signal to some slot and it was called just after few miliseconds. It would solve the problem but i need synchronous wait.

    I have also tried using nested QEventLoop and connecting socket readyRead() signal to quit() slot, but that doesn't work either.

    Only working solution for me was using waitForReadyRead() with small timeout inside a while loop and check for required timeout with QElapsedTimer:
    QElapsedTimer tm;
    while ( ((socket->bytesAvailable()+ socket->encryptedBytesAvailable()) < size)
    && !tm.hasExpired(timeout))

    if (socket->bytesAvailable() + socket->encryptedBytesAvailable() >= size) {
    qDebug() << "OK";

    Why waitForReadyRead() does not work here as expected? Any idea what i'm doing wrong?

    Before i have stiched from QTcpSocket to QSslSocket my code works well.
    I'm running Qt 5.3.1 compiled from source on Beaglebone (armv7).

    I have found i should only check bytesAvailable(), not encryptedBytesAvailable().
    It seems data is received inside QSslSocket buffer, but they are not decrypted until control gets back to event loop.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Glad you found out and thanks for sharing.

    Can you also update the thread title prepending [solved], so other forum users may know as solution has been found :)

  • Hi, it's not solved at all :)
    If waitForReadyRead() is used, it still blocks until timeout and returns false.
    Firstly i thought the return value of waitForReadyRead() is not correct because i got some data (bytesAvailable() + encryptedBytesAvailable() > 0). Later i have found i can't read data if they are still encrypted.

    After waitForReadyRead() timeouts, bytesAvailable() is still 0.
    I have only some encryptedBytesAvailable.

    So the real problem is:
    When waitForReadyRead() is used, received bytes are stored only in plain socket buffer and they will not be decrypted until control gets back to EventLoop.

    This happens when waitForReadyRead() is called just after write() and waitForBytesWritten(). If it's called from slot connected to readyRead() signal, it seems to work ok.

    My setup is rather simple. I have client which write message to socket and synchronously waits for response. On the other side, server has connected slot to socket readyRead(). Client and server are using same util methods for reading message (first 4 bytes is message length, rest is data). Only difference is that server calls waitForReadyRead() from slot. Server works, client not.

  • I have created bug repport: "here":https://bugreports.qt-project.org/browse/QTBUG-40900

    I have proposed solution which seems to work, but i'm not sure if it's correct.

  • Lifetime Qt Champion

    Good, thanks for sharing the link

Log in to reply