Getting TCP state list

Using GetTcpTable(), information about opened TCP ports in the local windows machine can be obtained. In this page, usage of GetTcpTable() is shown.

Sample code

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


#include <stdio.h>

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

int
main()
{
 DWORD i;
 PMIB_TCPTABLE pTcpTable;
 DWORD dwSize = 0;
 DWORD dwRetVal = 0;

 char *addr_ptr;
 unsigned short *port_ptr;

 /* Get size required by GetTcpTable() */
 if (GetTcpTable(NULL, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
   pTcpTable = (MIB_TCPTABLE *) malloc (dwSize);
 }

 /* Get actual data using GetTcpTable() */
 if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, 0))
      == NO_ERROR) {
   if (pTcpTable->dwNumEntries > 0) {
     for (i=0; i<pTcpTable->dwNumEntries; i++) {
       addr_ptr = (char *)&pTcpTable->table[i].dwLocalAddr;
       printf("Local Address:  %s\n",
         inet_ntoa(*(struct in_addr *)addr_ptr));

       port_ptr = (unsigned short *)&pTcpTable->table[i].dwLocalPort;
       printf("Local Port:     %ld\n",
         htons(*port_ptr));

       addr_ptr = (char *)&pTcpTable->table[i].dwRemoteAddr;
       printf("Remote Address: %s\n",
         inet_ntoa(*(struct in_addr *)addr_ptr));

       port_ptr = (unsigned short *)&pTcpTable->table[i].dwRemotePort;
       printf("Remote Port:    %ld\n",
         htons(*port_ptr));

       printf("State:          %ld\n", pTcpTable->table[i].dwState);
       printf("\n");
     }
   }
 } else {
   printf("GetTcpTable failed.\n");
   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 );
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
Local Address:  0.0.0.0
Local Port:     445
Remote Address: 0.0.0.0
Remote Port:    45262
State:          2

Local Address:  0.0.0.0
Local Port:     135
Remote Address: 0.0.0.0
Remote Port:    2176
State:          2

Local Address:  0.0.0.0
Local Port:     1025
Remote Address: 0.0.0.0
Remote Port:    8332
State:          2

Local Address:  0.0.0.0
Local Port:     1029
Remote Address: 0.0.0.0
Remote Port:    2096
State:          2

Structure used by GetTcpTable()

The following structure is MIB_TCPTABLE that is used by GetIpNetTable().


typedef struct _MIB_TCPROW {
  DWORD dwState;
  DWORD dwLocalAddr;
  DWORD dwLocalPort;
  DWORD dwRemoteAddr;
  DWORD dwRemotePort;
} MIB_TCPROW, *PMIB_TCPROW;

typedef struct _MIB_TCPTABLE {
  DWORD dwNumEntries;
  MIB_TCPROW table[ANY_SIZE];
} MIB_TCPTABLE, *PMIB_TCPTABLE;

MIB_TCPROW

dwState TCP state.
dwLocalAddr Local IP address(IPv4).
dwLocalPort Local TCP port.
dwRemoteAddr Remote IP address(IPv4).
dwRemotePort Remote TCP port.

MIB_TCPTABLE

dwNumEntries Number of TCP entries.
table TCP entry information is stored as array of MIB_TCPROW. The length of array is shown in dwNumEntries.

  

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