私は好奇心から、ここでパケット キャプチャ コードを見てきました。次のようなセクションがあります。
private void OnReceive(IAsyncResult ar)
{
try
{
int nReceived = mainSocket.EndReceive(ar);
//Analyze the bytes received...
ParseData (byteData, nReceived);
if (bContinueCapturing)
{
byteData = new byte[4096];
//Another call to BeginReceive so that we continue to receive the incoming
/packets
mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(OnReceive), null);
}
}
...
...
}
MSDN のドキュメントによると、EndReceive は実際に受信したバイト数を返しますが、各非同期受信後にnReceivedを連続して合計しても、予想されるバイト数に近くなりません。たとえば、16 MB のファイルをダウンロードしても、約 200K にしか達しませんでした。
私はこれに似た他の質問を見ましたが、何も見つかりませんでした。バッファ サイズを変更して、違いが生じるかどうかを確認してみましたが、違いはありませんでした。コードの機能を単に誤解しているだけですか?
編集:受信したバイトはそのように蓄積されています。かなり単純に思えるので、うまくいけば間違いはありません。
long totalBytes = 0;
Object byteLock = new Object();
private void ParseData(byte[] byteData, int nReceived)
{
lock (byteLock)
{
totalBytes += nReceived;
}
}
Edit2 : データを受信するために使用されるコードは次のとおりです。詳細が必要な場合は、質問の冒頭にあるリンクから完全なソースを入手できます。ファイルは MJsnifferForm.cs です。
private void OnReceive(IAsyncResult ar)
{
try
{
int nReceived = mainSocket.EndReceive(ar);
//Analyze the bytes received...
ParseData (byteData, nReceived);
if (bContinueCapturing)
{
byteData = new byte[4096];
//Another call to BeginReceive so that we continue to receive the incoming
//packets
mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(OnReceive), null);
}
}
catch (ObjectDisposedException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
「mainSocket.EndReceive」の呼び出しと「mainSocket.BeginReceive」の次の呼び出しの間に受信が失われる可能性があるかどうか疑問に思っていましたが、それは問題になるとは思いませんか?