|
-
Oct 10th, 2009, 11:59 AM
#1
Thread Starter
Addicted Member
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
-
Oct 11th, 2009, 05:05 AM
#2
Thread Starter
Addicted Member
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
-
Oct 11th, 2009, 10:58 PM
#3
Lively Member
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.
-
Oct 12th, 2009, 05:24 AM
#4
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.
-
Oct 12th, 2009, 06:24 AM
#5
Lively Member
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
-
Oct 12th, 2009, 07:08 AM
#6
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..
-
Oct 12th, 2009, 01:14 PM
#7
Thread Starter
Addicted Member
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
-
Oct 12th, 2009, 01:49 PM
#8
Thread Starter
Addicted Member
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
-
Oct 12th, 2009, 05:36 PM
#9
Thread Starter
Addicted Member
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
-
Oct 13th, 2009, 03:59 AM
#10
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.
-
Oct 13th, 2009, 02:26 PM
#11
Thread Starter
Addicted Member
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
-
Oct 14th, 2009, 09:25 AM
#12
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.
-
Oct 14th, 2009, 11:54 AM
#13
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|