Následující úryvek kódu vypíše seznam použitých TCP/UDP portů.

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
 
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
 
int main(int argc, char *argv[]){
  DWORD i, dwTcpSize = sizeof(MIB_TCPTABLE), dwUdpSize = sizeof(MIB_UDPTABLE);
  PMIB_TCPTABLE pTcp;
  pTcp = (MIB_TCPTABLE *)malloc(dwTcpSize);
  PMIB_UDPTABLE pUdp;
  pUdp = (MIB_UDPTABLE *)malloc(dwUdpSize);
  struct in_addr iaIP;
 
  if(pTcp == NULL){
    printf("Error: malloc - TCP1\n");
    return 1;
  }
 
  if(pUdp == NULL){
    printf("Error: malloc - UDP1\n");
    return 2;
  }
 
  if(GetTcpTable(pTcp, &dwTcpSize, TRUE) == ERROR_INSUFFICIENT_BUFFER){
    free(pTcp);
 
    pTcp = (MIB_TCPTABLE *)malloc(dwTcpSize);
 
    if(pTcp == NULL){
      printf("Error: malloc - TCP2\n");
      return 3;
    }
  }
 
  if(GetUdpTable(pUdp, &dwUdpSize, TRUE) == ERROR_INSUFFICIENT_BUFFER){
    free(pUdp);
 
    pUdp = (MIB_UDPTABLE *)malloc(dwUdpSize);
 
    if(pUdp == NULL){
      printf("Error: malloc - UDP2\n");
      return 4;
    }
  }
 
  if(GetTcpTable(pTcp, &dwTcpSize, TRUE) != NO_ERROR){
    free(pTcp);
    printf("Error: GetTcpTable - TCP\n");
    return 5;
  }
 
  if(GetUdpTable(pUdp, &dwUdpSize, TRUE) != NO_ERROR){
    free(pUdp);
    printf("Error: GetTcpTable - UDP\n");
    return 6;
  }
 
  printf("TCP\n----------------------------------------\n");
  for (i = 0; i < (int)pTcp->dwNumEntries; i++) {
    iaIP.S_un.S_addr = (u_long)pTcp->table[i].dwLocalAddr;
    printf("\tLocal Address: %s:%d\n", inet_ntoa(iaIP), ntohs((u_short)pTcp->table[i].dwLocalPort));
    iaIP.S_un.S_addr = (u_long)pTcp->table[i].dwRemoteAddr;
    printf("\tRemote Address: %s:%d\n", inet_ntoa(iaIP), ntohs((u_short)pTcp->table[i].dwRemotePort));
    printf("\tState: ");
 
    switch (pTcp->table[i].dwState) {
      case MIB_TCP_STATE_CLOSED:     // 1
        printf("CLOSED");
        break;
      case MIB_TCP_STATE_LISTEN:     // 2
        printf("LISTEN");
        break;
      case MIB_TCP_STATE_SYN_SENT:   // 3
        printf("SYN-SENT");
        break;
      case MIB_TCP_STATE_SYN_RCVD:   // 4
        printf("SYN-RECEIVED");
        break;
      case MIB_TCP_STATE_ESTAB:      // 5
        printf("ESTABLISHED");
        break;
      case MIB_TCP_STATE_FIN_WAIT1:  // 6
        printf("FIN-WAIT-1");
        break;
      case MIB_TCP_STATE_FIN_WAIT2:  // 7
        printf("FIN-WAIT-2 ");
        break;
      case MIB_TCP_STATE_CLOSE_WAIT: // 8
        printf("CLOSE-WAIT");
        break;
      case MIB_TCP_STATE_CLOSING:    // 9
        printf("CLOSING");
        break;
      case MIB_TCP_STATE_LAST_ACK:   // 10
        printf("LAST-ACK");
        break;
      case MIB_TCP_STATE_TIME_WAIT:  // 11
        printf("TIME-WAIT");
        break;
      case MIB_TCP_STATE_DELETE_TCB: // 12
        printf("DELETE-TCB");
        break;
      default:
        printf("UNKNOWN");
        break;
    }
    printf("\n\n");
  }
 
  free(pTcp);
 
  printf("UDP\n----------------------------------------\n");
  for (i = 0; i < (int)pUdp->dwNumEntries; i++) {
    iaIP.S_un.S_addr = (u_long)pUdp->table[i].dwLocalAddr;
    printf("\tLocal Address: %s:%d\n\n", inet_ntoa(iaIP), ntohs((u_short)pTcp->table[i].dwLocalPort));
  }
 
  free(pUdp);
 
  printf("Press ENTER...\n");
  getchar();
 
  return 0;    
}