MIB_TCPTABLE2 結構 (tcpmib.h)
MIB_TCPTABLE2結構包含本機電腦上的 IPv4 TCP 連線資料表。
語法
typedef struct _MIB_TCPTABLE2 {
DWORD dwNumEntries;
MIB_TCPROW2 table[ANY_SIZE];
} MIB_TCPTABLE2, *PMIB_TCPTABLE2;
成員
dwNumEntries
資料表中的專案數。
table[ANY_SIZE]
實作為 MIB_TCPROW2 結構陣列之 TCP 連線資料表的指標。
備註
GetTcpTable2函式會擷取本機電腦上的 IPv4 TCP 連線資料表,並在MIB_TCPTABLE2結構中傳回這項資訊。 MIB_TCPROW2結構的陣列包含在MIB_TCPTABLE2結構中。
MIB_TCPTABLE2結構可能包含dwNumEntries成員與資料表成員中第一個MIB_TCPROW2陣列專案之間的對齊填補。 表格成員中MIB_TCPROW2陣列專案之間也可能有對齊的填補。 任何對 MIB_TCPROW2 陣列專案的存取都應該假設填補可能存在。
範例
下列範例會擷取 IPv4 的 TCP 連接資料表做為 MIB_TCPTABLE2 結構,列印以 MIB_TCPROW2 結構表示的每個線上狀態。
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
// Need to link with Iphlpapi.lib and Ws2_32.lib
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
int main()
{
// Declare and initialize variables
PMIB_TCPTABLE2 pTcpTable;
ULONG ulSize = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
struct in_addr IpAddr;
int i;
pTcpTable = (MIB_TCPTABLE2 *) MALLOC(sizeof (MIB_TCPTABLE2));
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return 1;
}
ulSize = sizeof (MIB_TCPTABLE);
// Make an initial call to GetTcpTable2 to
// get the necessary size into the ulSize variable
if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE2 *) MALLOC(ulSize);
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return 1;
}
}
// Make a second call to GetTcpTable2 to get
// the actual data we require
if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) == NO_ERROR) {
printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
printf("\n\tTCP[%d] State: %ld - ", i,
pTcpTable->table[i].dwState);
switch (pTcpTable->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("CLOSING\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:
wprintf(L"UNKNOWN dwState value: %d\n", pTcpTable->table[i].dwState);
break;
}
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
printf("\tTCP[%d] Local Port: %d \n", i,
ntohs((u_short)pTcpTable->table[i].dwLocalPort));
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
printf("\tTCP[%d] Remote Port: %d\n", i,
ntohs((u_short)pTcpTable->table[i].dwRemotePort));
printf("\tTCP[%d] Owning PID: %d\n", i, pTcpTable->table[i].dwOwningPid);
printf("\tTCP[%d] Offload State: %ld - ", i,
pTcpTable->table[i].dwOffloadState);
switch (pTcpTable->table[i].dwOffloadState) {
case TcpConnectionOffloadStateInHost:
printf("Owned by the network stack and not offloaded \n");
break;
case TcpConnectionOffloadStateOffloading:
printf("In the process of being offloaded\n");
break;
case TcpConnectionOffloadStateOffloaded:
printf("Offloaded to the network interface control\n");
break;
case TcpConnectionOffloadStateUploading:
printf("In the process of being uploaded back to the network stack \n");
break;
default:
printf("UNKNOWN Offload state value\n");
break;
}
}
} else {
printf("\tGetTcpTable2 failed with %d\n", dwRetVal);
FREE(pTcpTable);
return 1;
}
if (pTcpTable != NULL) {
FREE(pTcpTable);
pTcpTable = NULL;
}
return 0;
}
需求
最低支援的用戶端 | Windows Vista [僅限傳統型應用程式] |
最低支援的伺服器 | Windows Server 2008 [僅限傳統型應用程式] |
標頭 | tcpmib.h (包含 Iphlpapi.h) |