简体   繁体   English

NetworkInterface.GetIPv4Statistics()。BytesReceived - 它返回什么?

[英]NetworkInterface.GetIPv4Statistics().BytesReceived - What does it return?

What does that particular field return? 该特定领域的回报是什么? I want the the number of bytes received per second. 我想要每秒接收的字节数。 Should I rely on this? 我应该依靠这个吗?

I think you can use it that way: 我想你可以这样使用它:

long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime beginTime = DateTime.Now;

// do something

long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime endTime = DateTime.Now;

long recievedBytes = endValue - beginValue;
double totalSeconds = (endTime - beginTime).TotalSeconds;

var bytesPerSecond = recievedBytes / totalSeconds;

Code Snippet for periodically update 用于定期更新的代码段

private object _lockObj;
private long bytesPerSecond = 0;
private Timer _refreshTimer = new Timer { Interval = 1000 };

// do in ctor or some init method
_refreshTimer.Tick += _refreshTimer_Tick;
_refreshTimer.Enabled = true;

private void _refreshTimer_Tick(object sender, EventArgs e)
{
  ThreadPool.QueueUserItem(callback => 
  {
    long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
    DateTime beginTime = DateTime.Now;

    Thread.Sleep(1000);

    long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
    DateTime endTime = DateTime.Now;

    long recievedBytes = endValue - beginValue;
    double totalSeconds = (endTime - beginTime).TotalSeconds;

    lock(_lockObj)
    {
      bytesPerSecond = recievedBytes / totalSeconds;
    }
  });

}

You can combine this with some tracking to record the recieved Bytes over time 您可以将此与一些跟踪相结合,以记录随时间推移的已接收字节

NetworkInterface.GetIPv4Statistics().BytesReceived 

will show you the total number of bytes received for the given interface. 将显示给定接口收到的总字节数。

I dont think you can exactly use this to get the number of bytes received per second. 我不认为你可以准确地使用它来获得每秒接收的字节数。

Check This 检查

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipv4interfacestatistics.bytesreceived.aspx http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipv4interfacestatistics.bytesreceived.aspx

According to MSDN (as others have also pointed out) it Gets the number of bytes that were received on the interface. 根据MSDN(正如其他人也指出的那样),它获取接口上接收的字节数。

How does it behave if, for example, the network interface is disconnected and reconnected (eg a network cable is pulled out or a wireless connection drops out). 例如,如果网络接口断开连接并重新连接(例如,拔出网络电缆或无线连接断开),它的行为如何?

If you want a much more reliable way to capture packets, filter them, dissect them and only count the ones that matter, then have a look at http://sourceforge.net/projects/sharppcap/ 如果你想要一个更可靠的方法来捕获数据包,过滤它们,解剖它们并只计算重要的数据,那么看看http://sourceforge.net/projects/sharppcap/

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

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