Thread: Upd
View Single Post
Old 08-06-2014, 04:42 AM
jaccog's Avatar
jaccog jaccog is offline
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Default Upd

I'm communication with two applications using UDP sockets.
It works just fine...99% of the time, but once in a while I seem to miss a packet at the receiving side.

On the sending end, I check the number of bytes sent in my transmit routine:
SentBytes = Connection.SendTo(Data, Endpoint)
This is my reference for the number of bytes that have actually been sent.

On the receiving end however, I sometimes receive less packages. When counting the number of bytes received, it seems that I'm missing exactly the number of bytes as one transmission should be long.
I receive my data like this:
Setting-up the Async receive:
    Private Sub AwaitData()
        BeginReceiveFailed = False
            SyncLock ConnectionLock
                Dim aSyncState As New StateObject With {.ConnectionSocket = Connection, .RemoteEndpoint = New IPEndPoint(0, 0)}
                aSyncState.AsyncResult = Connection.BeginReceiveFrom(_RxBuffer, 0, _RxBuffer.Length - 1, SocketFlags.None, aSyncState.RemoteEndpoint, 
                                            New AsyncCallback(AddressOf ReceiveCallback), aSyncState)
            End SyncLock
        Catch ex As Exception
            BeginReceiveFailed = True
        End Try
    End Sub
The receivecallback is like this:
    Private Sub ReceiveCallback(ByVal AR As IAsyncResult)
        Dim aSyncState As StateObject = AR.AsyncState
        Dim ByteCount As Integer

            SyncLock ConnectionLock
                ByteCount = aSyncState.ConnectionSocket.EndReceiveFrom(AR, aSyncState.RemoteEndpoint)
            End SyncLock
            ProcessBytes(_RxBuffer, ByteCount, aSyncState.RemoteEndpoint)
            System.Threading.Interlocked.Add(_RxBytes, ByteCount)
        Catch ex As Exception
        End Try
    End Sub
The ProcessBytes routine handles my data (displaying, starting task, etc).
Could it be that my "ProcessBytes" routine takes too long so that the socket is unable to receive another packet while processing the previous packet?
Packets are max 100 bytes long.

I am on a local LAN with only a single switch, so I don't think the message gets lost in a colission.

Any clue as to why I am missing some messages?

Thanks a bunch!
-The one who says it cannot be done should never interrupt the one who is doing it-

Last edited by passel; 08-06-2014 at 11:04 AM. Reason: Wrapped a long line so easier to read forum page.
Reply With Quote