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. can I set the timeout value on QAbstractSocket::connectToHost()?

can I set the timeout value on QAbstractSocket::connectToHost()?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 6.2k Views 3 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    The title says it all -- I'm looking for a way to set the timeout value for connection attempts. In my system, connections should succeed within a second or two, or be cancelled. I don't believe there's a way to cancel a connection attempt, so the next best thing would be to have a short timeout.

    Thanks...

    JonBJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      The title says it all -- I'm looking for a way to set the timeout value for connection attempts. In my system, connections should succeed within a second or two, or be cancelled. I don't believe there's a way to cancel a connection attempt, so the next best thing would be to have a short timeout.

      Thanks...

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @mzimmers
      There isn't just a "variable" for such a timeout, you have to do it yourself in code.

      Assuming it works, the code at https://stackoverflow.com/a/25352019/489865 would seem to me to illustrate the principle. (Because waitForConnected() probably won't work reliably for you.)

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

        Hi,

        IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.

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

        JonBJ 2 Replies Last reply
        4
        • SGaistS SGaist

          Hi,

          IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @SGaist
          Very good addition! Would be nice if
          http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost
          mentioned
          http://doc.qt.io/qt-5/qnetworkconfiguration.html#setConnectTimeout
          :)

          1 Reply Last reply
          0
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            Would be even nicer if they bothered to mention the units for the argument to setConnectTimeout().

            http://doc.qt.io/qt-5/qnetworkconfiguration.html#setConnectTimeout

            Still good to know. I'll experiment with it right now.

            JonBJ 1 Reply Last reply
            0
            • mzimmersM mzimmers

              Would be even nicer if they bothered to mention the units for the argument to setConnectTimeout().

              http://doc.qt.io/qt-5/qnetworkconfiguration.html#setConnectTimeout

              Still good to know. I'll experiment with it right now.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @mzimmers
              It's an int :) Milliseconds I would hazard --- you'll soon find out!

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

                Yes, it's in milliseconds. I forgot to mention the unit in the patch.

                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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Fix is on its way.

                  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
                  1
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #9

                    From the docs:

                    *QNetworkConfiguration::QNetworkConfiguration()

                    Constructs an invalid configuration object.*

                    So...how do I go about making it valid?

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

                      Read the classe's details, it's explained there.

                      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
                      1
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #11

                        Είναι όλα ελληνικά για μένα.

                        Seriously, I've read that section a few times, and don't see how to establish a new configuration. Do I need to use a QNetworkConfigurationManager object?

                        JonBJ J.HilkJ 2 Replies Last reply
                        0
                        • mzimmersM mzimmers

                          Είναι όλα ελληνικά για μένα.

                          Seriously, I've read that section a few times, and don't see how to establish a new configuration. Do I need to use a QNetworkConfigurationManager object?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @mzimmers
                          While you're waiting for the master, you get the monkey....

                          Does https://code.woboq.org/qt5/qtbase/src/network/socket/qabstractsocket.cpp.html#1148 imply there a network session, you access QNetworkSession::configuration() and call your setConnectTimeout() on that to later it? Don't know how you know what the "current" session is, maybe from QNetworkConfigurationManager...? :)

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

                            Yes you need to.

                            As an alternative you could use a QTimer with the timeout you want and abort the connection.

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

                            kshegunovK 1 Reply Last reply
                            2
                            • SGaistS SGaist

                              Yes you need to.

                              As an alternative you could use a QTimer with the timeout you want and abort the connection.

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #14

                              @SGaist said in can I set the timeout value on QAbstractSocket::connectToHost()?:

                              As an alternative you could use a QTimer with the timeout you want and abort the connection.

                              Indeed, because disconnecting it will probably hang [1].

                              [1]: https://bugreports.qt.io/browse/QTBUG-72024

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Hi,

                                IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @SGaist said in can I set the timeout value on QAbstractSocket::connectToHost()?:

                                IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.

                                @kshegunov , @SGaist could we go back to this approach, please? It would be very easy to call http://doc.qt.io/qt-5/qnetworkconfiguration.html#setConnectTimeout instead of writing code. Trouble is, neither OP @mzimmers nor I know how we get from a QAbstractSocket instance to the QNetworkConfiguration instance needed to call that? I don't see statics, I don't think we create one ourselves, I presume we get the "current" network configuration for session object or something? I see QNetworkSession, do we live in a current one of those? The answer must be very simple, but we don't see it from the docs!

                                kshegunovK 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @SGaist said in can I set the timeout value on QAbstractSocket::connectToHost()?:

                                  IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.

                                  @kshegunov , @SGaist could we go back to this approach, please? It would be very easy to call http://doc.qt.io/qt-5/qnetworkconfiguration.html#setConnectTimeout instead of writing code. Trouble is, neither OP @mzimmers nor I know how we get from a QAbstractSocket instance to the QNetworkConfiguration instance needed to call that? I don't see statics, I don't think we create one ourselves, I presume we get the "current" network configuration for session object or something? I see QNetworkSession, do we live in a current one of those? The answer must be very simple, but we don't see it from the docs!

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  I've never used that, but as far I could tell it has nothing to do with the actual sockets themselves. From what I understood it's to manage the connectivity of the system (more or less).

                                  Read and abide by the Qt Code of Conduct

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

                                    An example of use can be found in the chat example

                                    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
                                    • mzimmersM mzimmers

                                      Είναι όλα ελληνικά για μένα.

                                      Seriously, I've read that section a few times, and don't see how to establish a new configuration. Do I need to use a QNetworkConfigurationManager object?

                                      J.HilkJ Online
                                      J.HilkJ Online
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by J.Hilk
                                      #18

                                      @mzimmers
                                      you could take a look at the FortuneCookieServer example
                                      http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html

                                      it makes use of the QNetrworkConfigurationManager and QAbstract(tcp)Sockets


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      0
                                      • mzimmersM Offline
                                        mzimmersM Offline
                                        mzimmers
                                        wrote on last edited by
                                        #19

                                        I decided to take SGaist's suggestion of using a timer, and aborting. Works fine and I can understand it.

                                        A note to anyone using something like this for a momentary switch: remember to kill the timer once you've reached your goal (in my case, connection status). If you don't, and you undo your goal (in my case, manually disconnecting) before the timer lapses, you'll get a bogus timeout.

                                            QTcpSocket *m_sock;
                                        	QTimer *m_pTimerConnect;
                                        ...
                                        Widget::Widget(QWidget *parent) :
                                            QWidget(parent),
                                            ui(new Ui::Widget)
                                        	...
                                        	m_pTimerConnect = new QTimer(this);
                                        	m_pTimerConnect->setSingleShot(true);
                                        	QObject::connect(m_pTimerConnect, &QTimer::timeout, this, &Widget::abortConnect);
                                        ...
                                        void Widget::sockConnect()
                                        {
                                        
                                            while (m_socketState != QAbstractSocket::UnconnectedState)
                                            {
                                                m_sock->disconnectFromHost();
                                                QThread::msleep(10);
                                            }
                                            m_pTimerConnect->start(m_timerIntervalConnect);
                                            m_sock->connectToHost(ui->lineEditAddr->text(),
                                                               (quint16) ui->lineEditPort->text().toInt(),
                                                               QIODevice::ReadWrite,
                                                               QAbstractSocket::IPv4Protocol);
                                        }
                                        ...
                                        void Widget::abortConnect()
                                        {
                                        	if (m_socketState != QAbstractSocket::ConnectedState)
                                        	{
                                        		m_sock->abort();
                                        		QMessageBox qmb(this);
                                        		qmb.setText("Connection attempt timed out.");
                                        		qmb.exec();
                                        	}
                                        }
                                        ...
                                        void Widget::onSocketStateChange(QAbstractSocket::SocketState socketState)
                                        {
                                        	m_socketState = socketState;
                                        	ui->lineEditState->setText(SocketStateText[socketState]);
                                        	if (m_socketState == QAbstractSocket::ConnectedState)
                                        	{
                                        		ui->pushButtonConnect->setText("Press to Disconnect");
                                        		// stop the connection timer.
                                        		m_pTimerConnect->stop();
                                        	}
                                        	else
                                        	{
                                        		ui->pushButtonConnect->setText("Press to Connect");
                                        	}
                                        }
                                        
                                        
                                        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