大网数据广告活动
网络与域名解析

Retrieving Information Using GetTcpStatistics

· 52CE 我爱测

The GetTcpStatistics function fills a pointer to an MIB_TCPSTATS structure with information on the TCP protocol statistics for the local computer.

The GetTcpStatistics function fills a pointer to an MIB_TCPSTATS structure with information on the TCP protocol statistics for the local computer.

To use GetTcpStatistics

  1. Declare some variables that are needed.

    Declare a DWORD variable dwRetVal that will be using for error checking function calls. Declare a pointer to an MIB_TCPSTATS variable called pTCPStats, and allocate memory for the structure. Check that memory could be allocated.

    DWORD dwRetVal = 0;
    PMIB_TCPSTATS pTCPStats;
    
    pTCPStats = (MIB_TCPSTATS *) malloc(sizeof (MIB_TCPSTATS));
    if (pTCPStats == NULL) {
        printf("Error allocating memory\n");
    }
    
  2. Call the GetTcpStatistics function with the pTCPStats parameter to retrieve TCP statistics for IPv4 on the local computer. Check for errors and return the error value in the DWORD variable dwRetVal. If an error occurs, the dwRetVal variable can be used for more extensive error checking and reporting.

        if ((dwRetVal = GetTcpStatistics(pTCPStats)) != NO_ERROR) {
            printf("GetTcpStatistics failed with error: %ld\n", dwRetVal);
        } 
    
  3. If the call was successful, access the data returned in the MIB_TCPSTATS pointed to by the pTCPStats parameter.

    printf("\tNumber of active opens:  %u\n", pTCPStats->dwActiveOpens);
    printf("\tNumber of passive opens: %u\n", pTCPStats->dwPassiveOpens);
    printf("\tNumber of segments received: %u\n", pTCPStats->dwInSegs);
    printf("\tNumber of segments transmitted: %u\n", pTCPStats->dwOutSegs);
    printf("\tNumber of total connections: %u\n", pTCPStats->dwNumConns);
    
  4. Free the memory allocated for the MIB_TCPSTATS structure pointed to by the pTCPStats parameter. This should be done once the application no longer needs the data returned by the pTCPStats parameter.

    if (pTCPStats)
        free(pTCPStats);
    

Next Step: Retrieving Information Using GetIpStatistics

Previous Step: Retrieving Information Using GetIpStatistics

Complete Source Code

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。