Async. Socket Begin/EndReceive Loop Woes
C#
1
Posts
1
Posters
0
Views
1
Watching
-
Hi, I'm trying to write a receive loop in my socket class that will receive what data is in the buffer, call an event to let the user know that there's new data, and then receive the rest of the data. It kinda almost works; it receives much of the data, but seems to miss the last packet or two. Here is the code in the loop:
private void OnSockReceive(IAsyncResult ar)
{try { int read = socket.EndReceive(ar); if (read > 0) { OnReceive(); } socket.BeginReceive(ReceivedData, 0, ReceivedData.Length, SocketFlags.None, OnSockReceive, null); } catch (Exception ex) { MessageBox.Show(ex.Message, "OnSockReceive", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
ReceivedData is a global byte array of size 1024. Any help would be greatly appreciated. Thanks.