【问题标题】:How to use GetTcpIPv4Statistics [closed]如何使用 GetTcpIPv4Statistics [关闭]
【发布时间】:2013-03-24 21:22:41
【问题描述】:

我想获取 TCP/UDP IPv4 统计信息。我搜索并找到了GetTcpIPv4Statistics API。那么,如何使用它在 c++ 代码中获取 TCP/UDP 统计信息?

【问题讨论】:

  • 您的具体问题是什么?
  • 我想通过使用 GetTcpIPv4Statistics 来获取 TCP 统计信息,与提供 netstat -s 的统计信息相同
  • 所以你想在原生 c++ 代码中使用 .net 方法,还是我弄错了?

标签: c++ windows winapi tcp ipv4


【解决方案1】:

这似乎只是一个 .net 方法,它调用底层的GetTcpStatisticsEx IP Helper API,这是您想从非托管 C++ 中使用的。

【讨论】:

    【解决方案2】:

    我解决了这个问题。为了获取 IPV4 的 TCP 统计信息,我使用了以下代码:

    #include "stdafx.h"
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    
    #pragma comment(lib, "iphlpapi.lib")
    
    #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 
    #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
    
    /* Note: could also use malloc() and free() */
    
    int main()
    {
        PMIB_TCPSTATS pTCPStats;
        DWORD dwRetVal = 0;
    
        pTCPStats = (MIB_TCPSTATS*) MALLOC (sizeof(MIB_TCPSTATS));
        if (pTCPStats == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    
        if ((dwRetVal = GetTcpStatisticsEx(pTCPStats,AF_INET)) == NO_ERROR) {
          printf("\tActive Opens: %ld\n", pTCPStats->dwActiveOpens);
          printf("\tPassive Opens: %ld\n", pTCPStats->dwPassiveOpens);
          printf("\tSegments Recv: %ld\n", pTCPStats->dwInSegs);
          printf("\tSegments Xmit: %ld\n", pTCPStats->dwOutSegs);
          printf("\tTotal # Conxs: %ld\n", pTCPStats->dwAttemptFails);
          printf("\tAttemp failed: %ld\n", pTCPStats->dwAttemptFails);
          printf("\tcurr estab: %ld\n", pTCPStats->dwCurrEstab);
          printf("\testab reset: %ld\n", pTCPStats->dwEstabResets);
          printf("\tIn err: %ld\n", pTCPStats->dwInErrs);
          printf("\tmax conn: %ld\n", pTCPStats->dwMaxConn);
          printf("\tNum conn: %ld\n", pTCPStats->dwNumConns);
          printf("\tout rst: %ld\n", pTCPStats->dwOutRsts);
          printf("\tretrans seg: %ld\n", pTCPStats->dwRetransSegs);
           printf("\tRtoAlgorithm: %ld\n", pTCPStats->dwRtoAlgorithm);
            printf("\RtoMax: %ld\n", pTCPStats->dwRtoMax);
             printf("\RtoMin: %ld\n", pTCPStats->dwRtoMin);
              printf("\RtoAlgorithm: %ld\n", pTCPStats->RtoAlgorithm);
        }
        else {
          printf("GetTcpStatistics failed with error: %ld\n", dwRetVal);
    
          LPVOID lpMsgBuf;
          if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM | 
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dwRetVal,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL )) {
            printf("\tError: %s", lpMsgBuf);
          }
          LocalFree( lpMsgBuf );
        }
    
        if (pTCPStats)
            FREE (pTCPStats);
        system("pause");
    }
    

    为了获得 IPv6 的统计信息,我只需将 AF_INET 替换为 AF_INET6

    【讨论】:

    • 老实说,经过仔细分析,返回的数据非常可疑。从那以后,我一直使用 ETW 跟踪作为更可靠的替代方案。在未来的某个时候,我会为此做一个教程。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2019-01-16
    • 2012-03-08
    • 2018-09-15
    • 2013-09-14
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多