欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼

 更新時(shí)間:2020年07月13日 14:54:59   作者:武小棧  
這篇文章主要介紹了C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

1、前言

  最近做項(xiàng)目需要用到監(jiān)測(cè)網(wǎng)速及流量,我經(jīng)過(guò)百度和墻內(nèi)谷歌都沒(méi)能快速發(fā)現(xiàn)監(jiān)測(cè)IPV6流量和網(wǎng)速的用例;也經(jīng)過(guò)自己的一番查詢和調(diào)試,浪費(fèi)了不少時(shí)間,現(xiàn)在作為經(jīng)驗(yàn)分享出來(lái)希望大家指正。

2、C#代碼

using System.Net.NetworkInformation;
using System.Timers;

namespace Monitor
{
  public class MonitorNetwork
  {   
    public string UpSpeed { get; set; }  
    public string DownSpeed { get; set; }
    public string AllTraffic { get; set; }      
    private string NetCardDescription { get; set; }  
    //建立連接時(shí)上傳的數(shù)據(jù)量
    private long BaseTraffic { get; set; }  
    private long OldUp { get; set; }  
    private long OldDown { get; set; }
    private NetworkInterface networkInterface { get; set; }
    private Timer timer = new Timer() { Interval = 1000 };
  
    public void Close()
    {
      timer.Stop();  
    }
  
    public MonitorNetwork(string netCardDescription)
    {  
      timer.Elapsed += Timer_Elapsed;  
      NetCardDescription = netCardDescription;  
      timer.Interval = 1000;   
    }

    public bool Start()
    {
      networkInterface = null;  
      NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();  
      foreach (var var in nics)
      {
        if (var.Description.Contains(NetCardDescription))
        {
          networkInterface = var;
          break;
        }
      }  
      if (networkInterface == null)
      {
        return false;
      }
      else
      {  
        BaseTraffic = (networkInterface.GetIPStatistics().BytesSent +
                networkInterface.GetIPStatistics().BytesReceived);  
        OldUp = networkInterface.GetIPStatistics().BytesSent;  
        OldDown = networkInterface.GetIPStatistics().BytesReceived;  
        timer.Start();  
        return true;
      }
  
    }

    private string[] units = new string[] {"KB/s","MB/s","GB/s" };

    private void CalcUpSpeed()
    {
      long nowValue = networkInterface.GetIPStatistics().BytesSent;  
      int num = 0;
      double value = (nowValue - OldUp) / 1024.0;
      while (value > 1023)
      {
        value = (value / 1024.0);
        num++;
      }  
      UpSpeed = value.ToString("0.0") + units[num];  
      OldUp = nowValue;  
    }
  
    private void CalcDownSpeed()
    {
      long nowValue = networkInterface.GetIPStatistics().BytesReceived;  
      int num = 0;
      double value = (nowValue - OldDown) / 1024.0;   
      while (value > 1023)
      {
        value = (value / 1024.0);
        num++;
      }  
      DownSpeed = value.ToString("0.0") + units[num];  
      OldDown = nowValue;  
    }
  
    private string[] unitAlls = new string[] { "KB", "MB", "GB" ,"TB"};
  
    private void CalcAllTraffic()
    {
      long nowValue = OldDown+OldUp;  
      int num = 0;
      double value = (nowValue- BaseTraffic) / 1024.0;
      while (value > 1023)
      {
        value = (value / 1024.0);
        num++;
      }  
      AllTraffic = value.ToString("0.0") + unitAlls[num];
    }

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
      CalcUpSpeed();
      CalcDownSpeed();
      CalcAllTraffic();
    }
  }
}

3、胡說(shuō)八道

  雖然沒(méi)能直接快速地百度到方法,但是實(shí)現(xiàn)這個(gè)需求的時(shí)候,心里是有個(gè)譜,Windows系統(tǒng)能監(jiān)測(cè)到這個(gè)網(wǎng)速和流量,沒(méi)理由實(shí)現(xiàn)不了,只需要一個(gè)方法將這個(gè)信息讀取出來(lái)就好。最后實(shí)現(xiàn)這個(gè)需求是利用了System.Net.NetworkInformation這個(gè)程序集,但是這個(gè)程序集沒(méi)有只接提供網(wǎng)速監(jiān)測(cè)的方法,而是提供了接收和發(fā)送數(shù)據(jù)量的屬性,需要自己計(jì)算出即使網(wǎng)速,所以這個(gè)網(wǎng)速不是特別的準(zhǔn)確。

  這個(gè)程序集其實(shí)一開(kāi)始就看到了,前輩方法中使用的是IPv4InterfaceStatistics類中的BytesReceived屬性和BytesSent屬性實(shí)現(xiàn)的,但是在這個(gè)程序集里沒(méi)有對(duì)應(yīng)的IPv6類,恍恍惚惚。

  然后呢,我就下意識(shí)以為這個(gè)程序集比較老舊,不支持IPv6統(tǒng)計(jì)信息讀取,然后也是各種搜索無(wú)果,之后呢不死心想再來(lái)研究研究,東點(diǎn)點(diǎn)西瞅瞅,然后在NetworkInterface 類中發(fā)現(xiàn)了一個(gè)GetIPStatistics()方法,它的描述是“獲取此 NetworkInterface 實(shí)例的 IP 統(tǒng)計(jì)信息?!?。

  然后就順理成章的事了,根據(jù)GetIPStatistics()返回的IPInterfaceStatistics實(shí)例中的BytesReceived屬性和BytesSent屬性就能獲取到收發(fā)的數(shù)據(jù)總量,然后根據(jù)這個(gè)信息就能計(jì)算出大約的網(wǎng)速。

  經(jīng)測(cè)試,利用IPInterfaceStatistics實(shí)例是能讀取到IPv4和IPv6的總數(shù)據(jù)量的,因?yàn)檫@次的需求就是監(jiān)測(cè)總量,如果需要單獨(dú)監(jiān)測(cè)IPv6的可以用總量減去IPv4部分。

4、后記

​  老師以前喊我認(rèn)真念書,我心想有百度還不夠嗎,再念能有百度聰明,有百度懂得多,后來(lái)漸漸明白,百度懂得多都是前輩的搬磚添瓦來(lái)的,共勉。

參考資料

  System.Net.NetworkInformation 命名空間

以上就是C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論