Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QTcpSocket cancel connectToHost

    General and Desktop
    2
    4
    2334
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      TT_NBE last edited by

      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

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        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 ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply Reply Quote 0
        • T
          TT_NBE last edited by

          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'

          1 Reply Last reply Reply Quote 0
          • dheerendra
            dheerendra Qt Champions 2022 last edited by

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

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply Reply Quote 0
            • First post
              Last post