Results 1 to 13 of 13

Thread: How do I detect a client disconnect

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    How do I detect a client disconnect

    Hi all,
    So I think I have made it to possibly my 3rd or 4th post, so I m still new to this.

    Anyway onto my question(s).

    Assume my client application has connected to my server correctly.

    Is it possible for the client application to do the following when closing?

    TcpClient.GetStream.Close()

    Will my server application see the stream has closed and if so will
    TcpCLient.GetStream.CanRead then be false.

    or how do I go about detecting a lost client?

    Sorry the question is vague.

    Regards

    Adrian

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    Looking at the forums it appears we dont have many network experts here.

    So after doing a bit more reading and googling it appears detecting client disconnect is not a straight forward thing to do.

    Once I get a satisfactory solution(more likely if), I will post it here, for people with a similiar issue.

    Regards

    Adrian

  3. #3
    Lively Member Raiser's Avatar
    Join Date
    Oct 2009
    Location
    Tamilnadu,India.
    Posts
    68

    Re: How do I detect a client disconnect

    Dear Friend,

    I have been struggling for past 15 days to find a solution for this issue. i am having the same problem in my first ever networking project. I have to detect the client's Status and Display as "Connected" or "Disconnected". But if some one shutdown or restarts the client system, we cannot get the state of the client. In this situation how can i find and display "Disconnected"?

    I think you also have the same problem. Have you got the answer?

    If you got the answer, Please Post it.. It could be helpful for me..

    Thanks.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do I detect a client disconnect

    The CanRead property does still return True even if the client has called Close on the connection so you cant use that property as a test. The Read method will return 0 though if there is an error reading from the stream so you can use that. Basically you just call the Read method and it will block until either some data is received (in which case you process it as you want to) or it will return 0 if there is a problem reading from the stream.

    As per MSDN:
    If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Lively Member Raiser's Avatar
    Join Date
    Oct 2009
    Location
    Tamilnadu,India.
    Posts
    68

    Re: How do I detect a client disconnect

    Dear Chris128,

    I am facing this issue in Vb 6.0.. You have provided the solution in .Net.... In .Net, we can handle the situation like this.. But in Vb 6.0, can it be possible? Because i need solution in VB.. So Please Kindly help me.

    Thanks in Advance

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do I detect a client disconnect

    I was answering the original question in this thread, if you have the same question but in a different language then post a new thread for it and specify what language you are using..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    So TcpCLient.GetStream.Read will throw an exception if the stream is closed or broken.

    I never actually get to that situation because I generally handle getting data from the Client stream by creating a loop then using TcpClinet.GetStream.DataAvailable to confirm there is data then use .Read to get it.

    Could use TcpClient.GetStream.Read and then catch the exception.

    Got a bit of time tonight. Time to play

    Regards

    Adrian

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    Ah the light has dawned. think I got it sorted now
    Will post some code when I tidy it up and make it ok for general consumption.

    Read and Understand. Iwill try to do this more often.

    As mentioned the TcpClient.GetStream.Read is blocking and waits until there is data available.

    If the Stream is broken or disconnected .Read returns 0 bytes straight away and throws an exception.

    Use Try Catch to handle this. Voila notification of disconnect..

    Have I got it now?

    Regards

    Adrian

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    Ok the code isnt brilliant. But this handles disconnect.

    Ill let the experts pull the code apart but I think the concept is sound.

    Code:
     Private Sub _Read()
            Const BUFFER_SIZE As Integer = 1024
            Dim bytesread As Integer
            Do
                Dim sbuilder As New System.Text.StringBuilder
                Do
                    Try
                        Dim bytes(BUFFER_SIZE) As Byte
                        bytesread = NetStream.Read(bytes, 0, BUFFER_SIZE)
                        ReDim Preserve bytes(bytesread - 1)
                        Dim partstring As String = Encoding.ASCII.GetString(bytes)
                        sbuilder.Append(partstring)
    
                    Catch ex As Exception
                        Console.WriteLine("CLIENT DISCONNECTED!")
                        Exit Sub
                    End Try
                Loop Until NetStream.DataAvailable = False
                RaiseEvent DataReceived(Me, sbuilder.ToString)
            Loop
    
        End Sub

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do I detect a client disconnect

    I cant see anything wrong with that code in general personally but one thing I'm pretty sure of is that the Read method doesnt throw an exception when the client disconnects. It simply returns 0. So instead of using that Try/Catch block to detect a disconnect (of course you should still use Try/Catch for error handling though) I would just test to see if the bytesread variable is equal to 0 and if it is then there was an error in the communication so you can assume the client has disconnected.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    I dont doubt you know your stuff when it comes to this sort of thing but (why is there always a but) if I close the client application the server throws this exception.

    A first chance exception of type 'System.IO.IOException' occurred in System.dll

    This might be because Im not properly closing the netstream from the client end but just doin a forceful disconnect.

    will try a few things first.

    Regards

    Adrian

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do I detect a client disconnect

    Ah well you didnt say closing the application before, you just said calling Close on the TcpClient You need to handle that exception just like you would any other, with a Try/Catch block. Then if that exception is thrown you can treat that in exactly the same way as you would treat the Read method returning 0 - i.e show the client as being disconnected.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: How do I detect a client disconnect

    Yup as always I dont give the whole story. Thanks for that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width