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
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.