-2

I want to get TCP/UDP IPv4 statistics. I serached and I found GetTcpIPv4Statistics API. SO, how to use it to get TCP/UDP statistics in a c++ code ?

user2219913
  • 582
  • 2
  • 5
  • 13

2 Answers2

5

That appears to be a .net method only, it calls the underlying GetTcpStatisticsEx IP Helper API which is what you want to use from unmanaged C++.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
4

I solved the problem. To get TCP statistics for IPV4, I used this code :

#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");
}

and to get statistics for IPv6, I just replace AF_INET by AF_INET6

user2219913
  • 582
  • 2
  • 5
  • 13
  • Honestly though, after careful analysis, the data that is returned is very suspect. I have since used ETW tracing as a more reliable alternative. At some point in the future I will do a tutorial for this. – Malcolm Swaine May 27 '19 at 20:51