[經驗棧]C#監測IPv4v6網速及流量
阿新 • • 發佈:2020-06-18
# 1、前言
最近做專案需要用到監測網速及流量,我經過百度和牆內谷歌都沒能快速發現監測IPV6流量和網速的用例;也經過自己的一番查詢和除錯,浪費了不少時間,現在作為經驗分享出來希望大家指正。
# 2、C#程式碼
```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; }
//建立連線時上傳的資料量
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、胡說八道
雖然沒能直接快速地百度到方法,但是實現這個需求的時候,心裡是有個譜,Windows系統能監測到這個網速和流量,沒理由實現不了,只需要一個方法將這個資訊讀取出來就好。最後實現這個需求是利用了[System.Net.NetworkInformation](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation?redirectedfrom=MSDN&view=netframework-4.7.2)這個程式集,但是這個程式集沒有隻接提供網速監測的方法,而是提供了接收和傳送資料量的屬性,需要自己計算出即使網速,所以這個網速不是特別的準確。
這個程式集其實一開始就看到了,前輩方法中使用的是[IPv4InterfaceStatistics](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipv4interfacestatistics?view=netframework-4.7.2)類中的[BytesReceived](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipv4interfacestatistics.bytesreceived?view=netframework-4.7.2#System_Net_NetworkInformation_IPv4InterfaceStatistics_BytesReceived)屬性和[BytesSent](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipv4interfacestatistics.bytessent?view=netframework-4.7.2#System_Net_NetworkInformation_IPv4InterfaceStatistics_BytesSent)屬性實現的,但是在這個程式集裡沒有對應的IPv6類,恍恍惚惚。
![IPv4InterfaceStatistics 類](https://img2020.cnblogs.com/blog/1487270/202006/1487270-20200618170744525-2019626660.png)
然後呢,我就下意識以為這個程式集比較老舊,不支援IPv6統計資訊讀取,然後也是各種搜尋無果,之後呢不死心想再來研究研究,東點點西瞅瞅,然後在[NetworkInterface](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.networkinterface?view=netframework-4.7.2) 類中發現了一個[GetIPStatistics()](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.networkinterface.getipstatistics?view=netframework-4.7.2#System_Net_NetworkInformation_NetworkInterface_GetIPStatistics)方法,它的描述是“獲取此 [NetworkInterface](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.networkinterface?view=netframework-4.7.2) 例項的 IP 統計資訊。”。
![NetworkInterface 類](https://img2020.cnblogs.com/blog/1487270/202006/1487270-20200618170819696-528014289.png)
然後就順理成章的事了,根據[GetIPStatistics()](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.networkinterface.getipstatistics?view=netframework-4.7.2#System_Net_NetworkInformation_NetworkInterface_GetIPStatistics)返回的[IPInterfaceStatistics](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipinterfacestatistics?view=netframework-4.7.2)例項中的[BytesReceived](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipinterfacestatistics.bytesreceived?view=netframework-4.7.2#System_Net_NetworkInformation_IPInterfaceStatistics_BytesReceived)屬性和[BytesSent](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipinterfacestatistics.bytessent?view=netframework-4.7.2#System_Net_NetworkInformation_IPInterfaceStatistics_BytesSent)屬性就能獲取到收發的資料總量,然後根據這個資訊就能計算出大約的網速。
![IPInterfaceStatistics 類](https://img2020.cnblogs.com/blog/1487270/202006/1487270-20200618170848389-1466333278.png)
經測試,利用[IPInterfaceStatistics](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.ipinterfacestatistics?view=netframework-4.7.2)例項是能讀取到IPv4和IPv6的總資料量的,因為這次的需求就是監測總量,如果需要單獨監測IPv6的可以用總量減去IPv4部分。
# 4、後記
老師以前喊我認真唸書,我心想有百度還不夠嗎,再念能有百度聰明,有百度懂得多,後來漸漸明白,百度懂得多都是前輩的搬磚添瓦來的,共勉。
# 參考資料
[System.Net.NetworkInformation 名稱空間](https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation?view=netframework-4.7.2)
[1]: https://www.wulinn.com/usr/uploads/2020/06/1157408144.png
[2]: https://www.wulinn.com/usr/uploads/2020/06/723919410.png
[3]: https://www.wulinn.com/usr/uploads/2020/06/2303533528.png