Qt: default connection timeout for QTcpSocket
Please advise how to set the default connection timeout without using the waitForConnected () blocking method? I noticed that the socket emits an error signal (QAbstractSocket :: SocketTimeoutError) after about 60 seconds, which I can handle as a timeout, but can this timeout be adjusted?
+3
user3948829
source
to share
1 answer
You can use QTimer
:
- Run it after you have called
connectToHost
. - You may want to reset the timer when the socket state changes from to , possibly with a different timeout if you want finer granular control over when the timeout occurs.
QAbstractSocket::HostLookupState
QAbstractSocket::ConnectingState
- If you get a connection, stop the timer or reset to use the send / receive timeout in a similar way.
- If you get a timer timeout
disconnectFromHost()
and maybe try again when you get a disconnect signal.
When developing, be sure to plug in stateChanged(...)
and error(...)
signal at least debug slots that just print arguments. This way, you will see when something happens in a way that you did not expect.
+6
source to share