can I set the timeout value on QAbstractSocket::connectToHost()?
-
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...
-
@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.) -
Hi,
IIRC, that value was hardcoded to 30 seconds. However you can change that using QNetworkConfiguration since 5.9.
-
@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
:) -
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.
-
Yes, it's in milliseconds. I forgot to mention the unit in the patch.
-
Fix is on its way.
-
Read the classe's details, it's explained there.
-
@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 yoursetConnectTimeout()
on that to later it? Don't know how you know what the "current" session is, maybe fromQNetworkConfigurationManager
...? :) -
@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].
-
@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 theQNetworkConfiguration
instance needed to call that? I don't seestatic
s, I don't think we create one ourselves, I presume we get the "current" network configuration for session object or something? I seeQNetworkSession
, do we live in a current one of those? The answer must be very simple, but we don't see it from the docs! -
An example of use can be found in the chat example
-
@mzimmers
you could take a look at the FortuneCookieServer example
http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.htmlit makes use of the QNetrworkConfigurationManager and QAbstract(tcp)Sockets
-
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"); } }