Question

I've been looking at the packet capturing code here out of curiosity. There's a section like this:

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);
        }
    }
    ...
    ...
}

The MSDN documentation says that EndReceive does indeed return the number of bytes received, but simply adding up nReceived continuously after each asynchronous receive doesn't total anywhere near the number of bytes I would expect. Downloading a file of 16 MB, for instance, only reached about 200K.

I've looked at other questions similar to this without finding anything. I tried varying the buffer size to see if that made a difference, but it did not. Am I simply misunderstanding what the code does?

Edit: bytes received are being accumulated like so. Seems pretty simple, so hopefully I didn't make a mistake there!

long totalBytes = 0;
Object byteLock = new Object();
private void ParseData(byte[] byteData, int nReceived)
{
    lock (byteLock)
    {
        totalBytes += nReceived;                
    }
}

Edit2: here is the code being used to receive the data. The full source is available from the link at the beginning of my question, if any more details are needed. The file is 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);
     }            
}

I was wondering if receives could go missing between the call of "mainSocket.EndReceive" and the next call of "mainSocket.BeginReceive" but I don't think that's supposed to be a concern?

Was it helpful?

Solution

Answering my own question for anyone who comes across it: my problem was a silent block by the firewall. Adding exceptions for the program (including the VS studio debug executable, i.e. MJSniff.vshost.exe) allowed incoming traffic to be viewed. Lesson learned for me: it's not always the code!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top