QTcpSocket cancel connectToHost



  • Hello,

    I have used a QTcpSocket in my application and the socket works.

    The only problem I still have that once the 'connectToHost' function is called, there is no way to cancel it, so the user has to wait untill a timeout occurs. I know from past experiences that setting a timeout too short can sometimes lead to problems (especially on mobile connections) so my approach has always been set a long timeout and give the user the ability to 'cancel' but with QTcpSocket I can't seem to get this done.

    I've tried abort(), close(), disconnectFromHost() but all three seem to have no effect when the socket is not connected.

    So anyone an idea as to how to cancel a pending connectToHost()

    Thanks in advance



  • Looking at the Qt source, this will continue to wait until timer expires. Either you need to delete the instance of Socket or call abort. Abort should take care. I have not experimented this.

    Also how are you calling abort(...) method ? If your app is single thread, whole application may be paused here. Are you trying to abort from another thread ?



  • I've tried you suggestion, and called abort, and this in effect aborts the pending connection.

    So my issue actually was: when calling abort on the not connected socket, it does not emit a signal. So I'll have to emit a signal myself to inform the UI that the socket is back in the 'disconnected state'



  • Try with error(...) or stateChanged(...) or aboutToClose(...) or disconnected signals. Either one of them will emit. According to the code disconnected(..) signal should emit.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.