C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼
1、前言
最近做項(xiàng)目需要用到監(jiān)測(cè)網(wǎng)速及流量,我經(jīng)過百度和墻內(nèi)谷歌都沒能快速發(fā)現(xiàn)監(jiān)測(cè)IPV6流量和網(wǎng)速的用例;也經(jīng)過自己的一番查詢和調(diào)試,浪費(fèi)了不少時(shí)間,現(xiàn)在作為經(jīng)驗(yàn)分享出來希望大家指正。
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、胡說八道
雖然沒能直接快速地百度到方法,但是實(shí)現(xiàn)這個(gè)需求的時(shí)候,心里是有個(gè)譜,Windows系統(tǒng)能監(jiān)測(cè)到這個(gè)網(wǎng)速和流量,沒理由實(shí)現(xiàn)不了,只需要一個(gè)方法將這個(gè)信息讀取出來就好。最后實(shí)現(xiàn)這個(gè)需求是利用了System.Net.NetworkInformation這個(gè)程序集,但是這個(gè)程序集沒有只接提供網(wǎng)速監(jiān)測(cè)的方法,而是提供了接收和發(fā)送數(shù)據(jù)量的屬性,需要自己計(jì)算出即使網(wǎng)速,所以這個(gè)網(wǎng)速不是特別的準(zhǔn)確。
這個(gè)程序集其實(shí)一開始就看到了,前輩方法中使用的是IPv4InterfaceStatistics類中的BytesReceived屬性和BytesSent屬性實(shí)現(xiàn)的,但是在這個(gè)程序集里沒有對(duì)應(yīng)的IPv6類,恍恍惚惚。

然后呢,我就下意識(shí)以為這個(gè)程序集比較老舊,不支持IPv6統(tǒng)計(jì)信息讀取,然后也是各種搜索無果,之后呢不死心想再來研究研究,東點(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)真念書,我心想有百度還不夠嗎,再念能有百度聰明,有百度懂得多,后來漸漸明白,百度懂得多都是前輩的搬磚添瓦來的,共勉。
參考資料
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)文章
C#中的SQLCommand命令與DbTransaction事務(wù)處理
這篇文章介紹了C#中的SQLCommand命令與DbTransaction事務(wù)處理,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C# Winform實(shí)現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)
基本上,所有的業(yè)務(wù)系統(tǒng)都會(huì)要求有導(dǎo)出的功能,所以這篇文章主要為大家介紹了如何利用Winform實(shí)現(xiàn)原生DataGridView的導(dǎo)出功能,需要的可以參考一下2023-07-07
VS2012 未找到與約束ContractName匹配的導(dǎo)出
這篇文章主要介紹了在更新的windows補(bǔ)丁后,Visual Studio 用戶可能無法打開或創(chuàng)建 C++ 或 JavaScript 文件或項(xiàng)目,小編的解決辦法,希望可以幫助到大家2018-04-04
C#實(shí)現(xiàn)金額轉(zhuǎn)換成中文大寫金額
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)金額轉(zhuǎn)換成中文大寫金額,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
VS2017使用Git進(jìn)行源代碼管理的實(shí)現(xiàn)
這篇文章主要介紹了VS2017使用Git進(jìn)行源代碼管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
C#開發(fā)Windows UWP系列之布局面板RelativePanel
這篇文章介紹了C#開發(fā)Windows UWP系列之布局面板RelativePanel,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#使用RestSharp實(shí)現(xiàn)封裝常用的http請(qǐng)求方法
這篇文章主要為大家詳細(xì)介紹了C#如何使用RestSharp實(shí)現(xiàn)封裝常用的http請(qǐng)求方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-02-02

