weixin_46027762 2023-07-05 09:12 采纳率: 64.7%
浏览 105
已结题

Windows下c++获取网卡的下行流量、丢包率、错包率

我想实时获取Windows网卡的下行流量、丢包率、错包率的信息,用c/c++语言

  • 写回答

5条回答 默认 最新

  • churuxu 2023-07-05 14:59
    关注

    img

    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock2.h>
    #include <windows.h>
    #include <netioapi.h>
    #include <locale.h>
    #include <inttypes.h>
    
    #pragma comment(lib, "iphlpapi.lib")
    
    void show_row(PMIB_IF_ROW2 row){
        if(!row->InterfaceAndOperStatusFlags.HardwareInterface)return;
        if(!row->InterfaceAndOperStatusFlags.ConnectorPresent)return;
        wprintf(L"%ls\n", row->Alias);
        wprintf(L"接收字节数 %"PRIu64"\n", row->InUcastOctets);
        wprintf(L"发送字节数 %"PRIu64"\n", row->OutUcastOctets);
        wprintf(L"接收单播包数 %"PRIu64"\n", row->InUcastPkts);
        wprintf(L"发送单播包数 %"PRIu64"\n", row->OutUcastPkts);
        wprintf(L"接收多播包数 %"PRIu64"\n", row->InNUcastPkts);
        wprintf(L"发送多播包数 %"PRIu64"\n", row->OutNUcastPkts);
        wprintf(L"接收错误包数 %"PRIu64"\n", row->InErrors);    
        wprintf(L"发送错误包数 %"PRIu64"\n", row->OutErrors);
        wprintf(L"接收丢包数 %"PRIu64"\n", row->InDiscards);    
        wprintf(L"发送丢包数 %"PRIu64"\n", row->OutDiscards);
    
        ULONG64 error = row->InErrors + row->OutErrors;
        ULONG64 discard = row->InDiscards + row->OutDiscards;
        ULONG64 total = row->InUcastPkts + row->OutUcastPkts + row->InNUcastPkts + row->OutNUcastPkts + error + discard;
        double error_rate = total?((double)error / (double)total):0;
        double discard_rate = total?((double)discard / (double)total):0;
        
        wprintf(L"错误率 %.4f\n", error_rate);
        wprintf(L"丢包率 %.4f\n", discard_rate);
        printf("\n");
    }
    
    void show_table(PMIB_IF_TABLE2 table){
        int i;
        PMIB_IF_ROW2 row;
        for (i = 0; i < table->NumEntries; i++){
            row = &table->Table[i];
            show_row(row);
        }
    }
    
    void get_if_status(){
        PMIB_IF_TABLE2 table;
        if(0 == GetIfTable2Ex(MibIfTableNormal, &table)){
            show_table(table);
            FreeMibTable(table);
        }else{
            printf("GetIfTable2Ex fail %d\n", (int)GetLastError());
        }
    }
    
    int main(){
        setlocale(0,"");
        while(1){
            get_if_status();        
            Sleep(2000);
            system("cls");
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 7月13日
  • 已采纳回答 7月5日
  • 创建了问题 7月5日

悬赏问题

  • ¥15 安霸cv22 + rtl8211f 千兆,udp传输丢包
  • ¥15 关于区块链和边缘环境搭建的相关问题
  • ¥15 windows远程桌面断卡重连软件卡顿问题
  • ¥30 Unity 实现扫描效果
  • ¥15 HbuilderX检测不到安卓模拟器
  • ¥15 这个main已经在filename.obj中定义是什么错 C语言
  • ¥15 关于#linux#的问题:exsi8.0系统 怎么更改web访问端口,不用80、443
  • ¥15 使用elementor设计样式
  • ¥15 谁能提供一个中文版的推销咨询网站连接?
  • ¥15 springboot项目程序启动报错