• 获取本机TCP连接状态信息


    转自:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366909(v=vs.85).aspx

     pasting

    #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_TCPTABLE pTcpTable;
        DWORD dwSize = 0;
        DWORD dwRetVal = 0;
    
        char szLocalAddr[128];
        char szRemoteAddr[128];
    
        struct in_addr IpAddr;
    
        int i;
    
        pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
        if (pTcpTable == NULL) {
            printf("Error allocating memory
    ");
            return 1;
        }
    
        dwSize = sizeof (MIB_TCPTABLE);
    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
        if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) ==
            ERROR_INSUFFICIENT_BUFFER) {
            FREE(pTcpTable);
            pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
            if (pTcpTable == NULL) {
                printf("Error allocating memory
    ");
                return 1;
            }
        }
    // Make a second call to GetTcpTable to get
    // the actual data we require
        if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
            printf("	Number of entries: %d
    ", (int) pTcpTable->dwNumEntries);
            for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
                printf("
    	TCP[%d] State: %ld - ", i,
                       pTcpTable->table[i].dwState);
                switch (pTcpTable->table[i].dwState) {
                case MIB_TCP_STATE_CLOSED:
                    printf("CLOSED
    ");
                    break;
                case MIB_TCP_STATE_LISTEN:
                    printf("LISTEN
    ");
                    break;
                case MIB_TCP_STATE_SYN_SENT:
                    printf("SYN-SENT
    ");
                    break;
                case MIB_TCP_STATE_SYN_RCVD:
                    printf("SYN-RECEIVED
    ");
                    break;
                case MIB_TCP_STATE_ESTAB:
                    printf("ESTABLISHED
    ");
                    break;
                case MIB_TCP_STATE_FIN_WAIT1:
                    printf("FIN-WAIT-1
    ");
                    break;
                case MIB_TCP_STATE_FIN_WAIT2:
                    printf("FIN-WAIT-2 
    ");
                    break;
                case MIB_TCP_STATE_CLOSE_WAIT:
                    printf("CLOSE-WAIT
    ");
                    break;
                case MIB_TCP_STATE_CLOSING:
                    printf("CLOSING
    ");
                    break;
                case MIB_TCP_STATE_LAST_ACK:
                    printf("LAST-ACK
    ");
                    break;
                case MIB_TCP_STATE_TIME_WAIT:
                    printf("TIME-WAIT
    ");
                    break;
                case MIB_TCP_STATE_DELETE_TCB:
                    printf("DELETE-TCB
    ");
                    break;
                default:
                    printf("UNKNOWN dwState value: %d
    ", 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("	TCP[%d] Local Addr: %s
    ", i, szLocalAddr);
    
                printf("	TCP[%d] Local Port: %d 
    ", 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("	TCP[%d] Remote Addr: %s
    ", i, szRemoteAddr);
    
                printf("	TCP[%d] Remote Port: %d
    ", i,
                       ntohs((u_short)pTcpTable->table[i].dwRemotePort));
            }
        } else {
            printf("	GetTcpTable failed with %d
    ", dwRetVal);
            FREE(pTcpTable);
            return 1;
        }
    
        if (pTcpTable != NULL) {
            FREE(pTcpTable);
            pTcpTable = NULL;
        }    
    
        return 0;    
    }
  • 相关阅读:
    手势识别 ios
    无题
    核心动画笔记
    Quartz2D的学习2
    Quartz2D的学习1
    NSURLsessionTask
    NSURLSession
    POST请求的两种方式
    网络第一天
    NSThread
  • 原文地址:https://www.cnblogs.com/xj2015/p/7212294.html
Copyright © 2020-2023  润新知