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