Qt: default connection timeout for QTcpSocket
12,478
You could use a QTimer
:
- Start it after you have called
connectToHost
. - You may want to reset the timer when socket state changes from
QAbstractSocket::HostLookupState
toQAbstractSocket::ConnectingState
, perhaps with different timeout, if you want more fine-grained control on when exactly will the timeout happen. - If you get connection, stop the timer, or reset it for similar send/receive timeout use.
- If you get timer timeout, do
disconnectFromHost()
and possibly do something like retry when you get disconnect signal.
When developing, make sure to connect the stateChanged(...)
and error(...)
signals at least to debug slots, which just print the arguments. That way you will see when something happens in a way you did not expect.
Author by
Admin
Updated on June 19, 2022Comments
-
Admin almost 2 years
Please advise how can I setup default connection timeout without using blocking waitForConnected() method? I noticed that socket emit error signal (QAbstractSocket::SocketTimeoutError) after about 60 seconds that I can handle as a timeout, but can this timeout be adjusted?