Getting TCP statistics

IPHLPAPI provides an API to obtain TCP statistics. In this page, usage of GetTcpStatistics() that can obtain TCP statistics is shown.

Sample code

The following sample code shows how to use GetTcpStatistics().


#include <stdio.h>

#include <winsock2.h>
#include <iphlpapi.h>

int
main()
{
 MIB_TCPSTATS tcpstats;

 if (GetTcpStatistics(&tcpstats) == NOERROR) {
   printf("dwRtoAlgorithm: %ld\n", tcpstats.dwRtoAlgorithm);
   printf("dwRtoMin:       %ld\n", tcpstats.dwRtoMin);
   printf("dwRtoMax:       %ld\n", tcpstats.dwRtoMax);
   printf("dwMaxConn:      %ld\n", tcpstats.dwMaxConn);
   printf("dwActiveOpens:  %ld\n", tcpstats.dwActiveOpens);
   printf("dwPassiveOpens: %ld\n", tcpstats.dwPassiveOpens);
   printf("dwAttemptFails: %ld\n", tcpstats.dwAttemptFails);
   printf("dwEstabResets:  %ld\n", tcpstats.dwEstabResets);
   printf("dwCurrEstab:    %ld\n", tcpstats.dwCurrEstab);
   printf("dwInSegs:       %ld\n", tcpstats.dwInSegs);
   printf("dwOutSegs:      %ld\n", tcpstats.dwOutSegs);
   printf("dwRetransSegs:  %ld\n", tcpstats.dwRetransSegs);
   printf("dwInErrs:       %ld\n", tcpstats.dwInErrs);
   printf("dwOutRsts:      %ld\n", tcpstats.dwOutRsts);
   printf("dwNumConns:     %ld\n", tcpstats.dwNumConns);
 } else {
   printf("GetTcpStatistics failed : %d\n", GetLastError());
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
dwRtoAlgorithm: 4
dwRtoMin:       300
dwRtoMax:       120000
dwMaxConn:      -1
dwActiveOpens:  832
dwPassiveOpens: 10
dwAttemptFails: 0
dwEstabResets:  29
dwCurrEstab:    0
dwInSegs:       487549
dwOutSegs:      253735
dwRetransSegs:  0
dwInErrs:       0
dwOutRsts:      71
dwNumConns:     5

Structure used by GetTcpStatistics()

The following structure is MIB_TCPSTATS that is used by GetTcpStatistics().


typedef struct _MIB_TCPSTATS {
  DWORD dwRtoAlgorithm;
  DWORD dwRtoMin;
  DWORD dwRtoMax;
  DWORD dwMaxConn;
  DWORD dwActiveOpens;
  DWORD dwPassiveOpens;
  DWORD dwAttemptFails;
  DWORD dwEstabResets;
  DWORD dwCurrEstab;
  DWORD dwInSegs;
  DWORD dwOutSegs;
  DWORD dwRetransSegs;
  DWORD dwInErrs;
  DWORD dwOutRsts;
  DWORD dwNumConns;
} MIB_TCPSTATS, *PMIB_TCPSTATS;

dwRtoAlgorithm RTO(Retransmission Time-Out) Algorithm Type
MIB_TCP_RTO_CONSTANTConstant Time-out
MIB_TCP_RTO_RSREMIL-STD-1778 Appendix B
MIB_TCP_RTO_VANJVan Jacobson's Algorithm
MIB_TCP_RTO_OTHEROther
dwRtoMin Minimum RTO. msec.
dwRtoMax Maximum RTO. msec.
dwMaxConn Maximum number of connections. When -1, variable length.
dwActiveOpens Number of Active Open. Number of connections that are in initialization state.
dwPassiveOpens Number of Passive Open. Number of listen state.
dwAttemptFails Number of failed connections.
dwEstabResets Number of reset connections.
dwCurrEstab Number of connections that are currently established.
dwInSegs Number of segments received.
dwOutSegs Number of segments sent.
dwRetransSegs Number of retransmitted segments.
dwInErrs Number of errors received.
dwOutRsts Number of segments with RST flag.
dwNumConns Number of all connections. The listen state connections are not be included.
  

Copyright (C) GeekPage.JP. All rights reserved.