How to enumerate bound UDP / TCP sockets on Windows in C -


Assume that you do not have access to socket managers.

  // requirement =    

Iphlpapi.lib and Ws2_32.lib to link with #include & lt; Winsock2.h & gt; # Include & lt; Ws2tcpip.h & gt; # Include & lt; Iphlpapi.h & gt; # Include & lt; Stdio.h & gt; Define #pragma comment (lib, "iphlpapi.lib") #pragma comment (lib, "ws2_32.lib") # MALLOC (x) Heepolok (GetProcessHeap), 0, (x)) #define FREE (x) Heepfri ( GetProcessHeap (), 0, (x)) / * Note: can also use malloc () and free () * / int main () {// declaration and variable start PMIB_TCPTABLE pTcpTable; DWORD dwSize = 0; DWORD dwRetVal = 0; Four szLocalAddr [128]; Four szRemoteAddr [128]; Struct in_addr IpAddr; Int i; PTcpTable = (MIB_TCPTABLE *) MALLOC (SizeOf (MIB_TCPTABLE)); If (pTcpTable == NULL) {printf ("Error in allocating memory \ n"); Return 1; } DwSize = sizeof (MIB_TCPTABLE); // GetTcpTable achieve the required size dwSize variables // initial call if ((dwRetVal = GetTcpTable (pTcpTable, and dwSize, TRUE)) == ERROR_INSUFFICIENT_BUFFER {FREE (pTcpTable); PTcpTable = (MIB_TCPTABLE *) MALLOC (dwSize); if (pTcpTable == NULL) {printf ( "\ n" error allocating memory); return 1;}} // GetTcpTable requires actual data due to receive a second call // get us ((dwRetVal = GetTcpTable (pTcpTable, and dwSize, TRUE)) == NO_ERER) {printf ( "\ t number of entries:% d \ N", (int) pTcpTable-> dwNumEntries) to; (i = 0; I & lt; (int) pTcpTable-> dwNumEntries; i ++) {Ip Addr.S_un.S_addr = (u_long) pTcpTable-> Table [i] .dwLocalAddr; Strcpy_s (szLocalAddr, sizeof (szLocalAddr), inet_ntoa (IpAddr)); IpAddr.S_un.S_addr = (u_long) pTcpTable- & gt; table [i]. DwRemoteAddr; Strcpy_s (szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa (IpAddr)); Printf ( "\ n \ tTCP [% d] state:% ld -", i, pTcpTable-> table [i]. Dwtate; switch (ptcpt-> table [i] .dwState) {case MIB_TCP_STATE_CLOSED: printf ("closed \ n"); break; Case MIB_TCP_STATE_LISTEN: printf ("LISTEN \ n"); break; Case MIB_TCP_STATE_SYN_SENT: printf ("SYN-SENT \ n"); break; Case MIB_TCP_STATE_SYN_RCVD: printf ("SYN-received \ n"); break; Case MIB_TCP_STATE_ESTAB: printf ("ESTABLISHED \ n"); break; Case MIB_TCP_STATE_FIN_WAIT1: printf ("FIN-WAIT-1 \ n"); break; Case MIB_TCP_STATE_FIN_WAIT2: printf ("FIN-WAIT-2 \ n"); break; Case MIB_TCP_STATE_CLOSE_WAIT: printf ("CLOSE-WAIT \ n"); break; Case MIB_TCP_STATE_CLOSING: printf ("Closed \ n"); break; Case MIB_TCP_STATE_LAST_ACK: printf ("LAST-ACK \ n"); break; Case MIB_TCP_STATE_TIME_WAIT: printf ("TIME-WAIT \ n"); break; Case MIB_TCP_STATE_DELETE_TCB: printf ("DELETE-TCB \ n"); break; Default: printf ("UNKNOWN dwState value \ n"); break; } Printf ("\ tTCP [% d] Local Addr:% s \ n", i, szLocalAddr); Printf ("\ tTCP [% d] local port:% d \ n", i, ntohs ((u_short) pTcpTable-> Table [i] .dwLocalPort); Printf ("\ tTCP [% d] Remote edit:% s \ n", i, szRemoteAddr); Printf ( "\ tTCP [% d] Remote port:% d \ n", i, ntohs ((u_short) pTcpTable- & gt; Table [i] .dwRemotePort);}} Else {printf ( "\ tGetTcpTable% d \ N ", failed with dwRetVal); free (pTcpTable); return1;} return 0;}


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -