Windows
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "iphlpapi.lib")
//获取电脑适配器信息
void getPAdapter() {
DWORD dwRetVal = 0;
PIP_ADAPTER_INFO pAdapterInfo;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
// 获取适配器信息缓冲区大小
if (GetAdaptersInfo(NULL, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("分配内存失败\n");
return;
}
}
else {
printf("获取适配器信息缓冲区大小失败\n");
return;
}
// 获取适配器信息.GetAdaptersInfo 函数用于获取计算机上所有网络适配器的信息。
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
printf("适配器描述:%s\n", pAdapter->Description);
printf("适配器索引:%u\n", pAdapter->Index);
printf("物理地址:%02X-%02X-%02X-%02X-%02X-%02X\n",
pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2],
pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5]);
printf("---------------------------------\n");
pAdapter = pAdapter->Next;
}
}
else {
printf("获取适配器信息失败,错误码:%d\n", dwRetVal);
}
free(pAdapterInfo);
}
//获取适配器的宽带速率(Mbps)
//index适配器索引号
double getAdapterBroad(int index){
// 获取适配器索引
DWORD adapterIndex = index;
// 获取接口的详细信息
MIB_IFROW ifRow;
ifRow.dwIndex = adapterIndex;
if (GetIfEntry(&ifRow) == NO_ERROR) {
// 获取接口的宽带速率
DWORD speed = ifRow.dwSpeed;
double speedSizeT = static_cast<double>(speed)/ 1000000.0;
return speedSizeT;
}
else {
std::cout << "无法获取宽带速率" << std::endl;
return -1;
}
}
//得到适配器发送的字节数。index:适配器索引
size_t getSendByte(int index) {
DWORD dwRetVal = 0;
MIB_IFROW ifRow;
// 获取适配器索引
ifRow.dwIndex = index; // 替换为你要查询的适配器索引
// 查询适配器的统计信息
if ((dwRetVal = GetIfEntry(&ifRow)) == NO_ERROR) {
int intValue = static_cast<int>(ifRow.dwOutOctets);
return intValue;
}
else {
printf("获取适配器统计信息失败,错误码:%d\n", dwRetVal);
return -1;
}
}
//得到适配器接收的字节数。index:适配器索引
size_t getReceiveByte(int index) {
DWORD dwRetVal = 0;
MIB_IFROW ifRow;
// 获取适配器索引
ifRow.dwIndex = index; // 替换为你要查询的适配器索引
// 查询适配器的统计信息
if ((dwRetVal = GetIfEntry(&ifRow)) == NO_ERROR) {
int intValue = static_cast<int>(ifRow.dwInOctets);
return intValue;
}
else {
printf("获取适配器统计信息失败,错误码:%d\n", dwRetVal);
return -1;
}
}
/*计算网络占用率,带宽利用率 = (实际使用带宽 / 可用带宽) * 100%
* 1 Mbps(兆比特每秒)等于 125,000 byte每秒。
* 参数:index适配器索引号
*/
double getNetUse(int index) {
size_t sendByte = getSendByte(index);
size_t receiveByte = getReceiveByte(index);
size_t totalByte = sendByte+ receiveByte;
Sleep(1000);
size_t sendByte1 = getSendByte(index);
size_t receiveByte1 = getReceiveByte(index);
size_t totalByte1 = sendByte1 + receiveByte1;
double res = (totalByte1- totalByte) * 100 / (getAdapterBroad(index) * 125000);
return res;
}
int main() {
//我的WiFi网络适配器的索引是6,可以调上面的函数 getPAdapter()查看
//getPAdapter();
std::cout << getNetUse(6) << std::endl;
return 0;
}
Linux
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <unistd.h>
double getTotalNetworkBytes() {
std::ifstream file("/proc/net/dev");
if (!file) {
std::cerr << "无法打开网络设备文件" << std::endl;
return 0.0;
}
std::string line;
std::getline(file, line); // 跳过标题行
double totalBytes = 0.0;
while (std::getline(file, line)) {
//std::istringstream 用于创建一个输入字符串流对象 iss,并将 line
//字符串作为其初始化参数。通过这样做,我们可以使用 iss 对象从 line 字符串中提取数据。
std::istringstream iss(line);
std::string interface;
double bytes;
// 提取接口名称和接收/发送字节数
//表达式将尝试从 iss 字符串流中提取一个字符串值,并将其存储到 interface 变量中。
iss >> interface;
for (int i = 0; i < 9; ++i) {
//从 iss 字符串流中连续提取9个数值,并将它们累加到 totalBytes 变量中。
iss >> bytes;
totalBytes += bytes;
}
}
return totalBytes;
}
//返回流量占比百分数的整数部分
double getNetworkUsagePercentage() {
double totalBytes = getTotalNetworkBytes();
// 延迟一段时间
sleep(1);
double newTotalBytes = getTotalNetworkBytes();
double usageBytes = newTotalBytes - totalBytes;
// 计算流量占比
double totalUsagePercentage = (usageBytes / newTotalBytes) * 100.0;
return totalUsagePercentage;
}
int main() {
double usagePercentage = getNetworkUsagePercentage();
std::cout << "网络流量占比: " << usagePercentage << "%" << std::endl;
return 0;
}