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

QTcpSocket misses WSAECONNREFUSED error during connection attempts, times out instead on Windows

    XMLWordPrintable

Details

    • cc32226e647854a02ecd0775787bb08b85e2a014 (qt/qtbase/5.12)

    Description

      #include <QObject>
      #include <QCoreApplication>
      #include <QTcpSocket>
      #include <iostream>
      
      class Handler : public QObject
      {
      	Q_OBJECT
      
      public:
      	Handler(QObject *parent) : QObject(parent) { }
      	~Handler() { }
      
      public slots:
      	void onConnected() { qDebug() << "Connected"; } 
      	void onError(QAbstractSocket::SocketError e) { qDebug() << "Error:" << e; }
      };
      
      int main(int argc, char **argv) {
      	QCoreApplication a(argc, argv);
      
      	Handler *handler = new Handler(nullptr);
      	QTcpSocket *socket = new QTcpSocket(nullptr);
      
      	QObject::connect(socket, SIGNAL(connected()), handler, SLOT(onConnected()));
      	QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), handler, SLOT(onError(QAbstractSocket::SocketError)));
      
      	socket->connectToHost("0.0.0.1", 65000);
      
      	return a.exec();
      }
      

      The expected output is "Error: QAbstractSocket::ConnectionRefusedError" after a short (<5 seconds) amount of time.
      The actual output is "Error: QAbstractSocket::SocketTimeoutError" after much longer (~30 seconds) amount of time

      However, adding line

      socket->waitForConnected();

      right after

      socket->connectToHost("0.0.0.1", 65000);

      makes the program to print the expected output in 5 seconds.

      Apparently waitForConnected() remembers to call select() with non-zero errfds when on Windows, but whatever monitors the socket state in the event loop forgets this.

      Attachments

        Issue Links

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

          Activity

            People

              vhilshei Volker Hilsheimer
              joker_vd Semyon Kholodnov
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes