Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-59215

[REG 5.5 -> 5.6] QAbstractSocket::socketDescriptor() is no longer set while in ConnectingState

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.6.0
    • Network: Sockets
    • None

    Description

      Since https://codereview.qt-project.org/102610 (Qt 5.5 change), socketDescriptor() returns -1 while in ConnectingState.

      This makes it impossible to implement a non-blocking non-event-loop-based connect (basically, polling).

      The application code that I'm looking at, and which worked in Qt3, Qt4 and Qt < 5.5, is calling connectToHost() followed by socketDescriptor() and then - possibly multiple times, if the function is called again - it calls select() to check for writability (with a 1ms timeout since 0 wouldn't work).

      In pseudo code,

      // Caller can call this multiple times, for polling (from a thread without an event loop)
      bool MySocket::connect(const QHostAddress & addr, quint16 port)
      {
          if (state() != ConnectingState) {
              QTcpSocket::connectToHost(addr, port);
          }
          if (state() == ConnectedState) {
              return true;
          }
          int socket_fd = socketDescriptor();
          int n = select(socket_fd + 1, NULL, &wset, NULL, &tval);
          if (n == 0) return false;
          if (n < 0) { report error, return false; }
          if (getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &sock_error, &len) != 0 || sock_error != 0) { report error, return false; }
          return true;
      }
      

      The whole approach no longer works now that socketDescriptor() is -1, but I see no alternative either.

      Calling waitForConnected(0 or 1) leads to a disconnect in case of timeout, so that function cannot be called for such polling.

      QNativeSocketEngine::connectToHost/waitForWrite looks interesting, but there's no way to ask for the engine of a QTcpSocket, right?

      Any chance to make socketDescriptor() useful again while in ConnectingState ?

      Alternatively something like a waitForConnected(0, DoNotDisconnectOnError) would help.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            cnn Qt Core & Network
            dfaure_kdab David Faure
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes