socket.isconnected state wrong

  1. 6 months ago

    dave d

    6 Dec 2017 Pre-Release Testers, Xojo Pro Australia

    I have a client server (windows desktop mac server), I want to know if there is a current connection between the two machines so I can update a UI to reflect the remote machine is connected.

    I use the following code

    For Each tcp As TCPSocket In activeSockets
    if systcp(tcp).IsConnected=true then
    for i as integer = 0 to listbox1.RowCount-1
    ' at this point it's alive!!!!
    end if

    if I close or break out of the client app then isconnected returns false (great)
    if I disable the network adapter on the remote machine, isconnected does not switch to false, even after some minutes waiting.
    Is there a way to confirm a connection has been dropped rather than sending data every few seconds to the client and timing how long it takes to reply?
    Or is there a better way to do this.

  2. dave d

    6 Dec 2017 Pre-Release Testers, Xojo Pro Australia

    Update: you can ignore the internal for loop that was to update the UI

  3. Mathias M

    is not verified 6 Dec 2017 Pre-Release Testers Bruges, Belgium

    Hi Dave

    I have no way to check as I'm on my phone ready to sleep, but did you check for errorcode 102 in the .LastErrorCode property? This one should be 102 if the connection if lost (by force or when disconnecting in a proper manner)

  4. tcp is slow to recognize that things are not connected on the other end - ie it may take minutes to realize this
    the fastest way to check is to try & send them some data and handle the failure

  5. dave d

    6 Dec 2017 Pre-Release Testers, Xojo Pro Australia

    Im sending a test packet, but I'm not seeing the error event called in the socket?

  6. Peter J

    is not verified 6 Dec 2017 KwaZulu-Natal, South Africa
    Edited 6 months ago

    I might be on the wrong track, but this might give you a clue as to why you must wait a long time in the scenario you give:

    TCP connections consist of two sockets, one on each end of the connection. When one side wants to terminate the connection, it sends an RST packet which the other side acknowledges and both close their sockets.

    Until that happens, however, both sides will keep their socket open indefinitely. This leaves open the possibility that one side may close their socket, either intentionally or due to some error, without informing the other end via RST. In order to detect this scenario and close stale connections the TCP Keep Alive process is used.

    If you pull out the remote network cable or some such, you won't get the RST. So Norman's answer is your best bet.

    EDIT - you might have to roll your own timeout for waiting for the response.

or Sign Up to reply!