Hello DaniWebbers,
I have the following classes in their respective namespaces; so far so good. When I have received data from the server, the AsyncTcpClient's DataReceived event gets raised and the Client's OnReceivedData method gets called. Also so far so good. The one thing that does not work (and which I cannot comprehend), is that the server never gets the data that was sent by SendMessage(...) called in the Select Case statement. However, if I add a Thread.Sleep(10) before calling SendMessage(...), the message is received and all is well. (This is also true if I raise yet another event, defined in the Client class, and add a handler in my main form).
My assumption is that I am attempting to send a message in the client's BeginReceive handler (OnReceive) before it actually finishes. If that is the case, how would suspending the thread for 10ms help? Is there some sort of racing condition that occurs? If so, how can I improve my code that I can send a message right away without having to suspsend the thread first?
TDF.Network
Public Class AsyncTcpClient
Public Event DataReceived(ByVal iData as Byte())
'snip...
Private Sub OnReceive(ByVal ar as IAsyncResult)
'snip...
RaiseEvent DataReceived(socketData.Data)
'snip...
End Sub
'snip...
End Class
TDF.TicTacToe
Imports TDF.Network
Imports System.Text
Public Class Client
Private WithEvents m_Client as AsyncTcpClient
'snip...
Private Sub OnRecievedData(ByVal iData As Byte()) Handles m_Client.ReceivedData
'snip...
Select Case 'msg received
'snip...
Case 'stg I need
SendMessage(...)
'snip...
End Select
End Sub
'snip...
Public Sub SendMessage(ByVal msg As String)
m_Client.SendData(Encoding.UTF8.GetBytes(msg))
End Sub
End Class