【问题标题】:How can I access netstat-like Ethernet statistics from a Windows program如何从 Windows 程序访问类似 netstat 的以太网统计信息
【发布时间】:2010-09-18 06:47:11
【问题描述】:

如何从 netstat -e 等 C/C++ 代码访问以太网统计信息?

Interface Statistics

                       Received            Sent

Bytes                      21010071        15425579
Unicast packets               95512           94166
Non-unicast packets           12510               7
Discards                          0               0
Errors                            0               3
Unknown protocols                 0

【问题讨论】:

    标签: c windows networking ethernet network-monitoring


    【解决方案1】:

    WMI 将提供这些读数:

    SELECT * FROM Win32_PerfFormattedData_Tcpip_IP
    SELECT * FROM Win32_PerfFormattedData_Tcpip_TCP
    SELECT * FROM Win32_PerfFormattedData_Tcpip_UDP
    SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP
    SELECT * FROM Win32_PerfFormattedData_Tcpip_Networkinterface
    

    这些类在 Windows XP 或更新版本中可用。您可能必须在 Windows 2000 上使用匹配的“Win32_PerfRawData”类,并在显示输出之前做更多的数学运算。

    在 MSDN 中查找 documentation on all of them

    【讨论】:

    • 我查看了文档,但不清楚如何使用此功能。您知道使用您列出的类的 C 或 C++ 中的任何好的示例吗?
    • MSDN 总是一个很好的起点。 WMI C++ Application Examples。 (我想你自己也可以找到的。)
    • 我确实看到了,但对我来说不是很清楚。我会再看一遍。谢谢。
    【解决方案2】:

    Windows IPHelper 函数中的GetIpStatistics 调用是网络统计的一个好起点。

    还有其他一些可能更便携的方法:-

    • SNMP。需要在计算机上启用 SNMP,但显然也可用于检索远程计算机的统计信息。
    • 将“netstat”的输出通过管道传输到您的应用程序中,然后从文本中取消选择值。

    【讨论】:

    • netstat 的输出是特定于语言环境和版本的。很好看。 :-D
    • 呵呵。好吧,它可能仍然比让 SNMP 正常工作更容易...... ;-)
    【解决方案3】:

    让我回答自己,就像我在另一个论坛上问过的一样。

    WMI 不错,但改用 IpHlpApi 更容易:

    #include <winsock2.h>
    #include <iphlpapi.h>
    
    int main(int argc, char *argv[])
    {
    
    PMIB_IFTABLE pIfTable;
    MIB_IFROW ifRow;
    PMIB_IFROW pIfRow = &ifRow;
    DWORD dwSize = 0;
    
    // first call returns the buffer size needed
    DWORD retv = GetIfTable(pIfTable, &dwSize, true);
    if (retv != ERROR_INSUFFICIENT_BUFFER)
        WriteErrorAndExit(retv);
    pIfTable = (MIB_IFTABLE*)malloc(dwSize);
    
    retv = GetIfTable(pIfTable, &dwSize, true);
    if (retv != NO_ERROR)
        WriteErrorAndExit(retv);
    
    // Get index
        int i,j;
        printf("\tNum Entries: %ld\n\n", pIfTable->dwNumEntries);
        for (i = 0; i < (int) pIfTable->dwNumEntries; i++)
        {
            pIfRow = (MIB_IFROW *) & pIfTable->table[i];
            printf("\tIndex[%d]:\t %ld\n", i, pIfRow->dwIndex);
            printf("\tInterfaceName[%d]:\t %ws", i, pIfRow->wszName);
            printf("\n");
            printf("\tDescription[%d]:\t ", i);
            for (j = 0; j < (int) pIfRow->dwDescrLen; j++)
                printf("%c", pIfRow->bDescr[j]);
            printf("\n");
            ...
    

    【讨论】:

    • 这就是我之前的建议。然后,您需要调用 GetIpStatistics 以获取所需的统计信息。
    • @Roddy 不,“netstat -e”的统计数据只需要调用 GetIfTable,为 Denes +1
    【解决方案4】:

    齐亚,

    来自http://en.wikipedia.org/wiki/Netstat

    在 Windows 平台上,netstat 信息可以通过 调用 GetTcpTable 和 IP Helper 中的 GetUdpTable 函数 API 或 IPHLPAPI.DLL。信息 返回的包括本地和远程 IP 地址、本地和远程端口,以及 (对于 GetTcpTable)TCP 状态代码。在 除了命令行 附带的 netstat.exe 工具 Windows,有基于 GUI 的 netstat 可用的程序。 在 Windows 平台上,此命令 仅在 Internet 时可用 协议 (TCP/IP) 协议是 作为组件安装在 网络适​​配器的属性 网络连接。

    CodeProject 上的 MFC 示例:http://www.codeproject.com/KB/applications/wnetstat.aspx

    【讨论】:

      【解决方案5】:

      您可能会找到一个可行的WMI performance counter,例如Win32_PerfRawData_Tcpip_NetworkInterface.

      【讨论】:

        【解决方案6】:

        见Google Groups,原创netstats源码已经贴过很多次了(win32 api)

        【讨论】:

          【解决方案7】:

          如上述答案所示,WMI 性能计数器包含一些数据。请注意,在更高版本的 Windows 中,性能计数器在 v4 和 v6 中被分解,因此查询是:

          从 Win32_PerfFormattedData_Tcpip_IPv4 中选择 *

          从 Win32_PerfFormattedData_Tcpip_TCPv4 中选择 *

          从 Win32_PerfFormattedData_Tcpip_UDPv4 中选择 *

          从 Win32_PerfFormattedData_Tcpip_ICMP 中选择 *

          从 Win32_PerfFormattedData_Tcpip_IPv6 中选择 *

          从 Win32_PerfFormattedData_Tcpip_TCPv6 中选择 *

          从 Win32_PerfFormattedData_Tcpip_UDPv6 中选择 *

          从 Win32_PerfFormattedData_Tcpip_ICMPv6 中选择 *

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-12-15
            • 2010-10-13
            • 1970-01-01
            • 2011-08-24
            • 1970-01-01
            • 2014-09-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多