Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Has this ever been solved? I'm having the same issue. Usually, a TCP connection will timeout after some period of time, but I've left mine for hours and it never times out.



  • Re: QTcpSocket doesn't timeout

    Has this ever been solved? I'm having the same issue. Usually, a TCP connection will timeout after some period of time, but I've left mine for hours and it never times out.

    sdl


  • Moderators

    @sdl1 The OS doesn't always report a failed network connection. I don't think this is a Qt thing but an underlying sockets thing.

    The only real way to be sure it is still connected is a periodic heartbeat check. Then when it fails you know your connection is dead and can close/reconnect.

    For my programs that I've done this I always make sure it misses a few heartbeats before I call it dead to account for packet loss and slow connections. The downside to that is you may think it's alive for a while. Mine was about 30s or so before I called it gone. What I did was had an indicator in the status bar (just a color circle/light) it was green when it was good, yellow when it had missed a heartbeat, orange when it missed multiples, and red would be disconnected.

    This was with raw sockets and not using Qt, so it's really not a Qt issue. It was before I used Qt.



  • @sdl1 , @ambershark

    Yes, connections may timeout or may never timeout. Can depend on client/server/OS/implementation...

    Be aware that @ambershark's suggestion of "heartbeat" is a double-edged sword, depending on what you are trying to achieve. If your objective is to see if the other side has "gone away" it is suitable. But if your objective is actually to allow timeout & connection close (and detect it) it will actually prevent (as far as possible) the connection from ever timing out as you keep sending packets which will maintain the connection so it doesn't get a chance to timeout close!


  • Moderators

    @JonB said in Has this ever been solved? I'm having the same issue. Usually, a TCP connection will timeout after some period of time, but I've left mine for hours and it never times out.:

    But if your objective is actually to allow timeout & connection close (and detect it)

    Yep that's true. My app where I did that was a real time stock trading platform. It needed to detect if the other side had gone away and let the user know or get it back ASAP.

    Financial people get very grumpy when they don't have real time data. :D They are not understanding of socket limitations.


Log in to reply