Properly handling socket disconnects...

LookitsPuck

Active member
Joined
Jan 23, 2007
Messages
37
Programming Experience
3-5
VB.NET:
Public Sub OnReceivedData(ByVal ar As IAsyncResult)
        Try
            Dim content As String
            Dim intEOFPos As Int32
            Dim bytesRead As Int32 = mSock.EndReceive(ar)
            If (bytesRead > 0) Then
                'There might be more data
                'Store the data received so far
                readString.Append(Encoding.ASCII.GetString(mBuffer, 0, bytesRead))
                'Check for EOF tag
                content = readString.ToString()
                intEOFPos = content.IndexOf(LINE_SEPERATOR)
                Do
                    intEOFPos = content.IndexOf(LINE_SEPERATOR)
                    If (intEOFPos > -1) Then
                        'If EOF detected, call LineReceived function for processing
                        RaiseEvent LineReceived(Me, content.Substring(0, intEOFPos))
                        'Delete readString
                        readString.Remove(0, intEOFPos + 1)
                        'Reset the message and find new intEOFPos
                        content = readString.ToString()
                        intEOFPos = content.IndexOf(LINE_SEPERATOR)
                    End If
                Loop Until intEOFPos = -1
                Dim receiveData As New AsyncCallback(AddressOf OnReceivedData)
                mSock.BeginReceive(mBuffer, 0, mBuffer.Length, SocketFlags.None, receiveData, Me)
            Else
                RaiseEvent ErrorReceived(Me, String.Format("Client {0}, disconnected at {1}", mSock.RemoteEndPoint, DateTime.Now()))
            End If
            'Catch ex As System.InvalidOperationException
            'Don't worry about it
        Catch ex As Exception
            RaiseEvent ErrorReceived(Me, String.Format("Client {0}, disconnected  at {1}- Reason: {2}", mSock.RemoteEndPoint, DateTime.Now(), ex.Message))
        End Try
    End Sub
Now, problem occurs is that sometimes when the client application closes, it doesn't disconnect the user. What I do is when UserErrorReceived, I do the appropriate cleanup and disconnect the user. But sometimes this doesn't execute, which means sometimes no data is being sent when they close it out. Is there a more reliable, server driven way to do this? Or will the client have to send a LOGOFF message to the socket server?

Problem with the client, is if the client doesn't close properly (i.e. end task, or computer shutdown), it will not send the LOGOFF message.
 
Property shutting down socket connection....

VB.NET:
Public Sub OnReceivedData(ByVal ar As IAsyncResult)
 
        Try
 
            Dim content As String
 
            Dim intEOFPos As Int32
 
            Dim bytesRead As Int32 = mSock.EndReceive(ar)
 
            If (bytesRead > 0) Then
 
                'There might be more data
 
                'Store the data received so far
 
                readString.Append(Encoding.ASCII.GetString(mBuffer, 0, bytesRead))
 
                'Check for EOF tag
 
                content = readString.ToString()
 
                intEOFPos = content.IndexOf(LINE_SEPERATOR)
 
                Do
 
                    intEOFPos = content.IndexOf(LINE_SEPERATOR)
 
                    If (intEOFPos > -1) Then
 
                        'If EOF detected, call LineReceived function for processing
 
                        RaiseEvent LineReceived(Me, content.Substring(0, intEOFPos))
 
                        'Delete readString
 
                        readString.Remove(0, intEOFPos + 1)
 
                        'Reset the message and find new intEOFPos
 
                        content = readString.ToString()
 
                        intEOFPos = content.IndexOf(LINE_SEPERATOR)
 
                    End If
 
                Loop Until intEOFPos = -1
 
                Dim receiveData As New AsyncCallback(AddressOf OnReceivedData)
 
                mSock.BeginReceive(mBuffer, 0, mBuffer.Length, SocketFlags.None, receiveData, Me)
 
            Else
 
                RaiseEvent ErrorReceived(Me, String.Format("Client {0}, disconnected at {1}", mSock.RemoteEndPoint, DateTime.Now()))
 
            End If
 
            'Catch ex As System.InvalidOperationException
 
            'Don't worry about it
 
        Catch ex As Exception
 
RaiseEvent ErrorReceived(Me, String.Format("Client {0}, disconnected  at {1}- Reason: {2}", mSock.RemoteEndPoint, DateTime.Now(), ex.Message))
 
        End Try
 
    End Sub

Now, problem occurs is that sometimes when the client application closes, it doesn't disconnect the user. What I do is when UserErrorReceived, I do the appropriate cleanup and disconnect the user. But sometimes this doesn't execute, which means sometimes no data is being sent when they close it out. Is there a more reliable, server driven way to do this? Or will the client have to send a LOGOFF message to the socket server?

Problem with the client, is if the client doesn't close properly (i.e. end task, or computer shutdown), it will not send the LOGOFF message. There are other times where it doesn't disconnect. Is there a full proof way to take care of this that anyone knows of?

Currently the app sends LOGOFF as well as the OnUserErrorReceived event as seen above.
 
Back
Top