Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSslSocket::waitForReadyRead() not working

QSslSocket::waitForReadyRead() not working

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dorny
    wrote on last edited by
    #1

    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;
    tm.start();
    while ( ((socket->bytesAvailable()+ socket->encryptedBytesAvailable()) < size)
    && !tm.hasExpired(timeout))
    {
    socket->waitForReadyRead(100);
    }

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

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

    Note:
    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).

    EDIT:
    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 :)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dorny
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dorny
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Good, thanks for sharing the link

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              comy
              wrote on last edited by
              #6

              "flush" is the answer for both read and write.

              Check out my comment here:

              https://bugreports.qt-project.org/browse/QTBUG-40900?focusedCommentId=268875&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-268875

              1 Reply Last reply
              0
              • C Offline
                C Offline
                comy
                wrote on last edited by
                #7

                "flush" is the answer for both read and write.

                Check out my comment here:

                https://bugreports.qt-project.org/browse/QTBUG-40900?focusedCommentId=268875&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-268875

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved