Some question aboout GetPerTcpConnectionEStats(),it dose not work correctly

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi!I am working with Microsoft Visual studio 2010,OS is Win7.I want to us
GetPerTcpConnectionEStats() to get my computers net flow.The GetPerTcpConnectionEStats() function is defined on Windows Vista and later. When I use this API,each net connections net flow are all the same.
Here is my code,thanks!
#include "stdafx.h"<br/>
#include <winsock2.h><br/>
#include <Windows.h><br/>
#include <ws2tcpip.h><br/>
#include <iphlpapi.h><br/>
#include <tcpestats.h><br/>
#include <stdlib.h><br/>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")<br/>
#pragma comment(lib, "ws2_32.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))<br/>
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
<br/>
int _tmain(int argc, _TCHAR* argv[])<br/>
{<br/>
// Declare and initialize variables<br/>
PMIB_TCPTABLE pTcpTable;<br/>
PMIB_TCPROW row;<br/>
DWORD dwSize = 0;<br/>
DWORD dwRetVal = 0;
char szLocalAddr[128];<br/>
char szRemoteAddr[128];
ULONG rosSize=0,rodSize=0;<br/>
ULONG winStatus;<br/>
PUCHAR ros=NULL,rod=NULL;
PTCP_ESTATS_DATA_ROD_v0 dataRod = {0};
struct in_addr IpAddr;
int i;
pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));<br/>
if (pTcpTable == NULL) {<br/>
printf("Error allocating memoryn");<br/>
return 1;<br/>
}
dwSize = sizeof (MIB_TCPTABLE);<br/>
rodSize=sizeof(TCP_ESTATS_DATA_ROD_v0);
// Make an initial call to GetTcpTable to<br/>
// get the necessary size into the dwSize variable<br/>
if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize,TRUE,AF_INET,TCP_TABLE_BASIC_ALL,0)) ==<br/>
ERROR_INSUFFICIENT_BUFFER) {<br/>
FREE(pTcpTable);<br/>
pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);<br/>
if (pTcpTable == NULL) {<br/>
printf("Error allocating memoryn");<br/>
return 1;<br/>
}<br/>
}<br/>
// Make a second call to GetTcpTable to get<br/>
// the actual data we require<br/>
if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize,TRUE,AF_INET,TCP_TABLE_BASIC_ALL,0)) == NO_ERROR) {<br/>
row=pTcpTable->table;<br/>
printf("tNumber of entries: %dn", (int) pTcpTable->dwNumEntries);<br/>
for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {<br/>
printf("ntTCP[%d] State: %ld - ", i,<br/>
pTcpTable->table.dwState);<br/>
switch (pTcpTable->table.dwState) {<br/>
case MIB_TCP_STATE_CLOSED:<br/>
printf("CLOSEDn");<br/>
break;<br/>
case MIB_TCP_STATE_LISTEN:<br/>
printf("LISTENn");<br/>
break;<br/>
case MIB_TCP_STATE_SYN_SENT:<br/>
printf("SYN-SENTn");<br/>
break;<br/>
case MIB_TCP_STATE_SYN_RCVD:<br/>
printf("SYN-RECEIVEDn");<br/>
break;<br/>
case MIB_TCP_STATE_ESTAB:<br/>
printf("ESTABLISHEDn");<br/>
break;<br/>
case MIB_TCP_STATE_FIN_WAIT1:<br/>
printf("FIN-WAIT-1n");<br/>
break;<br/>
case MIB_TCP_STATE_FIN_WAIT2:<br/>
printf("FIN-WAIT-2 n");<br/>
break;<br/>
case MIB_TCP_STATE_CLOSE_WAIT:<br/>
printf("CLOSE-WAITn");<br/>
break;<br/>
case MIB_TCP_STATE_CLOSING:<br/>
printf("CLOSINGn");<br/>
break;<br/>
case MIB_TCP_STATE_LAST_ACK:<br/>
printf("LAST-ACKn");<br/>
break;<br/>
case MIB_TCP_STATE_TIME_WAIT:<br/>
printf("TIME-WAITn");<br/>
break;<br/>
case MIB_TCP_STATE_DELETE_TCB:<br/>
printf("DELETE-TCBn");<br/>
break;<br/>
default:<br/>
printf("UNKNOWN dwState valuen");<br/>
break;<br/>
}
IpAddr.S_un.S_addr = (u_long) pTcpTable->table.dwLocalAddr;<br/>
strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));<br/>
printf("tTCP[%d] Local Addr: %sn", i, szLocalAddr);<br/>
printf("tTCP[%d] Local Port: %d n", i,<br/>
ntohs((u_short)pTcpTable->table.dwLocalPort));
IpAddr.S_un.S_addr = (u_long) pTcpTable->table.dwRemoteAddr;<br/>
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));<br/>
printf("tTCP[%d] Remote Addr: %sn", i, szRemoteAddr);<br/>
printf("tTCP[%d] Remote Port: %dn", i,<br/>
ntohs((u_short)pTcpTable->table.dwRemotePort));<br/>
<br/>
if(rodSize!=0){<br/>
rod=(PUCHAR)malloc(rodSize);<br/>
if(rod==NULL){<br/>
printf("Out of Memory");<br/>
}<br/>
}
winStatus = GetPerTcpConnectionEStats(row,TcpConnectionEstatsData,NULL,0,0,NULL,0,0,rod,0,rodSize);
if(winStatus==NO_ERROR)<br/>
{<br/>
dataRod = (PTCP_ESTATS_DATA_ROD_v0) rod;<br/>
printf("tDatan");<br/>
printf("tBytes Out: %lun",dataRod->DataBytesOut);<br/>
printf("tBytes In : %lun",dataRod->DataBytesIn);<br/>
}<br/>
++row;<br/>
}<br/>
} else {<br/>
printf("tGetTcpTable failed with %dn", dwRetVal);<br/>
FREE(pTcpTable);<br/>
return 1;<br/>
}
if (pTcpTable != NULL) {<br/>
FREE(pTcpTable);<br/>
pTcpTable = NULL;<br/>
}
system("PAUSE");<br/>
return 0;<br/>
}

View the full article
 
Back
Top