Here's the code on the sending side of my sockets:
Private Sub Send(ByRef Buffer() As Byte)
Dim obj_StateObject As New StateObject
obj_StateObject.WorkSocket = m_tmpSocket
m_tmpSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnClientSendComplete), obj_StateObject)
End Sub
Private Sub OnClientSendComplete(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
End Sub
Here's the receiving code:
Private Sub OnServerDataArrival(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
Dim BytesRead As Integer = obj_Socket.EndReceive(ar)
If (BytesRead > 0) Then
Dim TempLength As Integer = BitConverter.ToInt32(obj_SocketState.Buffer, 0) - 4
Dim TempBuffer(TempLength - 1) As Byte
Array.Copy(obj_SocketState.Buffer, 4, TempBuffer, 0, TempLength)
RaiseEvent ServerDataArrival(TMP_ID, TempBuffer)
End If
obj_Socket.BeginReceive(obj_SocketState.Buffer, 0, obj_SocketState.BufferSize, 0, New AsyncCallback(AddressOf OnServerDataArrival), obj_SocketState)
End Sub
I can send and receive data just fine usually. For instance, I can send a 10 byte buffer across with a button click and there are no problems.
However, if I set that button to call a function that will loop the sending of the 10 byte buffer 10 times, I will not receive about 8 of the buffers on average.
What is causing this? The sending code is executed 10 times, but the OnServerDataArrival function is only executed 2 times.