c++ - How to get number of connections on PORT per IP in Windows? -


i have been searching around google time , can`t seem find answer...

i using getextendedtcptable() view current connections, unable number of connections each ip on specific port.

is there example/function ? or have create ?

any guidance or example appreciated.

have nice day!

so using this code msdn basis end example enumerates list of connections, populating pointer int ports number of connections remote systems local ports.

to determine number of connections particular local port, can print out value of ports[port].

the code supplied more c, c++, used new[] , delete[] ports.

#include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #include <stdio.h>  #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "ws2_32.lib")  #define malloc(x) calloc(1, (x)) #define free(x) free((x))  int main() {     // declare , initialize variables     pmib_tcptable_owner_pid ptcptable;     int *ports; // array of possible ports     dword dwsize = 0;     dword dwretval = 0;      char szremoteaddr[128];      struct in_addr ipaddr;      int i;      ports = new int[1 << (sizeof(u_short) * 8)]();      ptcptable = (mib_tcptable_owner_pid *)malloc(sizeof(mib_tcptable_owner_pid));     if (ptcptable == null) {         printf("error allocating memory\n");         return 1;     }      dwsize = sizeof(mib_tcptable);     // make initial call gettcptable     // necessary size dwsize variable     if ((dwretval = getextendedtcptable(ptcptable, &dwsize, true, af_inet, tcp_table_basic_connections, 0)) ==         error_insufficient_buffer) {         free(ptcptable);         ptcptable = (mib_tcptable_owner_pid *)malloc(dwsize);         if (ptcptable == null) {             printf("error allocating memory\n");             return 1;         }     }     // make second call gettcptable     // actual data require     if ((dwretval = getextendedtcptable(ptcptable, &dwsize, true, af_inet, tcp_table_basic_connections, 0)) == no_error) {         printf("\tnumber of entries: %d\n", (int)ptcptable->dwnumentries);         (i = 0; < (int)ptcptable->dwnumentries; i++) {             printf("\n\ttcp[%d] state: %ld - ", i,                 ptcptable->table[i].dwstate);             if (ptcptable->table[i].dwstate != mib_tcp_state_estab)                 continue;              // port in host order             u_short port = ntohs((u_short)ptcptable->table[i].dwlocalport);             ports[port] += 1; // increment port              ipaddr.s_un.s_addr = (u_long)ptcptable->table[i].dwremoteaddr;             inet_ntop(af_inet, &ipaddr, szremoteaddr, sizeof(szremoteaddr));             printf("\ttcp[%d] remote addr: %s:%d\n", i, szremoteaddr, ntohs((u_short)ptcptable->table[i].dwremoteport));         }     }     else {         printf("\tgetextendedtcptable failed %d\n", dwretval);         free(ptcptable);         delete[] ports;         return 1;     }      if (ptcptable != null) {         free(ptcptable);         ptcptable = null;     }     delete[] ports;     return 0; } 

now if wanted list of remote addresses coming port, make std::vector<std::string> remote_addresses , perform remote_addresses.push_back(szremoteaddr) when determined port correct.

there potential race condition between initial call getextendedtcptable , subsequent call getextendedtcptable connection comes in, increases value of dwsize. asking more memory helps in case prevent being issue - i.e. after initial call, try asking 8k more need - it's not big issue, though.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -