-1

I am using Vb.Net and VS 2013, based on my research it isn't possible to detect whether the client is still connected or not unless the client sends a KEEP ALIVE command to my server or sends a string indicating that that client will be disconnected before it disconnects itself, due to this I wanted to know if it is possible to detect if a client disconnects itself from my server? I mean if the client willingly disconnects it self from the server?

This is a follow up question to this wherein I am asking if it is possible to detect currently connected clients to my server

Edit: I've added that it is also possible by telling the client to send a string of command indicating that it will disconnect itself before the client actually disconnects itself.

Community
  • 1
  • 1
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
  • "based on my research it isn't possible to detect whether the client is still connected or not unless the client sends a KEEP ALIVE command to my server" --- well that simply wrong. Once clients disconnet they send a TCP RESET packet, and if they GET disconnected they simply timeout on the server-side. This timeout can be artificially shortened with TCP KEEPALIVE. Thats about it. – specializt Aug 13 '15 at 07:07
  • 1
    It seems like this question is just a rewording of you other question: if you can detect whether or not someone is still connected, you can detect them disconnecting (by repeatedly checking if they are still connected). Likewise, if you can detect them disconnecting, you can know if they are still connected. Can you explain what more information you need than the (correct) answer to your other question? – jwg Aug 13 '15 at 07:08
  • @jwg My previous question states if it is possible to detect `current`, whereas this question states if it is possible to detect clients who `willingly`. I think those are 2 different situation right? – Cary Bondoc Aug 13 '15 at 07:19
  • @specializt Thanks, yeah I forgot to mention that it is also possible. Please see my edit. – Cary Bondoc Aug 13 '15 at 07:22

1 Answers1

1

I think you should do a few tutorials on the subject :

https://msdn.microsoft.com/en-us/library/vstudio/ms172757(v=vs.100).aspx

and have a look at the documentation :
https://msdn.microsoft.com/en-us/library/vstudio/system.net.sockets.socket(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
https://msdn.microsoft.com/en-us/library/vstudio/1011kecd(v=vs.100).aspx

You can check the current state of the socket via Poll although i would recommend switching to an OOP approach

'Creates the Socket for sending data over TCP.
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' Connects to host using IPEndPoint.
s.Connect(EPhost)
If Not s.Connected Then
   strRetPage = "Unable to connect to host"
End If
' Use the SelectWrite enumeration to obtain Socket status.
If s.Poll(- 1, SelectMode.SelectWrite) Then
   Console.WriteLine("This Socket is writable.")
Else
   If s.Poll(- 1, SelectMode.SelectRead) Then
      Console.WriteLine(("This Socket is readable. "))
   Else
      If s.Poll(- 1, SelectMode.SelectError) Then
         Console.WriteLine("This Socket has an error.")
      End If
   End If 
End If 

If SelectMode.SelectError is True your client/server has either disconnected or timed out.

specializt
  • 1,913
  • 15
  • 26
  • What should I do wtih these 2 errors? 1. `'strRetPage' is not declared. It may be inaccessible due to its protection level.` 2. `'EPhost' is not declared. It may be inaccessible due to its protection level.` – Cary Bondoc Aug 13 '15 at 09:06
  • If the peer has disconnected cleanly the socket will become readable and reading it will return zero. No error. – user207421 Jan 06 '16 at 18:50