简体   繁体   English

.NET Socket.BeginReceive:读取的字节数

[英].NET Socket.BeginReceive: number of bytes read

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. MSDN文档说,EndReceive确实返回了接收到的字节数,但是在每次异步接收之后简单地连续累加nReceived并不会达到我期望的字节数。 Downloading a file of 16 MB, for instance, only reached about 200K. 例如,下载16 MB的文件仅达到约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. Edit2 :这是用于接收数据的代码。 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. 该文件是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? 我想知道在调用“ mainSocket.EndReceive”和下一个调用“ mainSocket.BeginReceive”之间是否可能会丢失接收,但是我不认为这应该成为问题吗?

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, ie MJSniff.vshost.exe) allowed incoming traffic to be viewed. 为程序添加例外(包括VS Studio调试可执行文件,即MJSniff.vshost.exe)可以查看传入的流量。 Lesson learned for me: it's not always the code! 我的经验教训:这并不总是代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM