Getting UDP statistics

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

Sample code

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

#include <stdio.h>

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

 MIB_UDPSTATS udpstats;

 if (GetUdpStatistics(&udpstats) == NOERROR) {
   printf("dwInDatagrams:  %ld\n", udpstats.dwInDatagrams);
   printf("dwNoPorts:      %ld\n", udpstats.dwNoPorts);
   printf("dwInErrors:     %ld\n", udpstats.dwInErrors);
   printf("dwOutDatagrams: %ld\n", udpstats.dwOutDatagrams);
   printf("dwNumAddrs:     %ld\n", udpstats.dwNumAddrs);
 } else {
   printf("GetUdpStatistics failed : %d\n", GetLastError());

 return 0;

Sample code output

The sample will output an message like the following.

C:> a.exe
dwInDatagrams:  59859
dwNoPorts:      2736
dwInErrors:     0
dwOutDatagrams: 2001
dwNumAddrs:     9

Structure used by GetUdpStatistics()

The following structure is MIB_UDPSTATS that is used by GetUdpStatistics().

typedef struct _MIB_UDPSTATS {
  DWORD dwInDatagrams;
  DWORD dwNoPorts;
  DWORD dwInErrors;
  DWORD dwOutDatagrams;
  DWORD dwNumAddrs;

dwInDatagrams Number of received datagrams.
dwNoPorts Number of datagrams received to a port with no service. The datagrams received are discarded.
dwInErrors Number of input errors. The ones that are counted in dwNoPorts are not included.
dwOutDatagrams Number of output datagrams.
dwNumAddrs Number of entries in UDP listener table.

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