利用函数来获得本机所有的端口

#include <windows.h>
#include <tchar.h>
#include <iphlpapi.h>
#include <stdlib.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "ws2_32.lib")

/*
typedef enum
{
	TCP_TABLE_BASIC_LISTENER,
	TCP_TABLE_BASIC_CONNECTIONS,
	TCP_TABLE_BASIC_ALL,
	TCP_TABLE_OWNER_PID_LISTENER,
	TCP_TABLE_OWNER_PID_CONNECTIONS,
	TCP_TABLE_OWNER_PID_ALL,
	TCP_TABLE_OWNER_MODULE_LISTENER,
	TCP_TABLE_OWNER_MODULE_CONNECTIONS,
	TCP_TABLE_OWNER_MODULE_ALL
} TCP_TABLE_CLASS, *PTCP_TABLE_CLASS;


typedef enum
{
    UDP_TABLE_BASIC,
    UDP_TABLE_OWNER_PID,
    UDP_TABLE_OWNER_MODULE,
} UDP_TABLE_CLASS, *PUDP_TABLE_CLASS;

*/

typedef DWORD(WINAPI * PFN_GET_EXTENDED_TCP_TABLE)
(
PVOID pTcpTable,
PDWORD pdwSize,
BOOL bOrder,
ULONG ulAf,
TCP_TABLE_CLASS TableClass,
ULONG Reserved
);

typedef DWORD(WINAPI * PFN_GET_EXTENDED_UDP_TABLE)
(
PVOID pUdpTable,
PDWORD pdwSize,
BOOL bOrder,
ULONG ulAf,
UDP_TABLE_CLASS TableClass,
ULONG Reserved
);

BOOL printTcp()
{
    int iErrno;
    PMIB_TCPTABLE_OWNER_PID pMibTcpTableOwnerPid;
    DWORD dwSize = 0;
    TCHAR szBuffer[1024];
    int i;
    HMODULE hModule;
    PFN_GET_EXTENDED_TCP_TABLE GetExtendedTcpTable;

    hModule = LoadLibrary(_T("iphlpapi.dll"));
    GetExtendedTcpTable = (PFN_GET_EXTENDED_TCP_TABLE)GetProcAddress(hModule, "GetExtendedTcpTable");
    if ((iErrno = GetExtendedTcpTable(NULL, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
    {
        if (iErrno != ERROR_INSUFFICIENT_BUFFER)
        {
            wsprintf(szBuffer, _T("GetExtendedTcpTable Error: %d\n"), iErrno);
            OutputDebugString(szBuffer);
            return FALSE;
        }
    }
    pMibTcpTableOwnerPid = (PMIB_TCPTABLE_OWNER_PID)malloc(dwSize);
    if (pMibTcpTableOwnerPid == NULL)
    {
        OutputDebugString(_T("malloc Error!\n"));
        return FALSE;
    }
    if ((iErrno = GetExtendedTcpTable(pMibTcpTableOwnerPid, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
    {
        wsprintf(szBuffer, _T("GetExtendedTcpTable Error: %d\n"), iErrno);
        OutputDebugString(szBuffer);
        return FALSE;
    }

    for (i = 0; i < (int)pMibTcpTableOwnerPid->dwNumEntries; i++)
    {
        IN_ADDR localAddr;
        IN_ADDR remoteAddr;
        TCHAR szLocalAddr[1024];
        TCHAR szRemoteAddr[1024];
        USHORT usLocalPort;
        USHORT usRemotePort;
        TCHAR szState[1024];
        DWORD dwWriteNum;
        TCHAR szLocal[1024];
        TCHAR szRemote[1024];

        localAddr.S_un.S_addr = pMibTcpTableOwnerPid->table[i].dwLocalAddr;
        remoteAddr.S_un.S_addr = pMibTcpTableOwnerPid->table[i].dwRemoteAddr;
        MultiByteToWideChar(CP_ACP, 0, inet_ntoa(localAddr), -1, szLocalAddr, 1024);
        MultiByteToWideChar(CP_ACP, 0, inet_ntoa(remoteAddr), -1, szRemoteAddr, 1024);
        switch (pMibTcpTableOwnerPid->table[i].dwState)
        {
        case MIB_TCP_STATE_CLOSED:
            wsprintf(szState, _T("%s"), _T("CLOSED"));
            break;
        case MIB_TCP_STATE_LISTEN:
            wsprintf(szState, _T("%s"), _T("LISTENING"));
            break;
        case MIB_TCP_STATE_SYN_SENT:
            wsprintf(szState, _T("%s"), _T("SYN_SENT"));
            break;
        case MIB_TCP_STATE_SYN_RCVD:
            wsprintf(szState, _T("%s"), _T("SYN_RCVD"));
            break;
        case MIB_TCP_STATE_ESTAB:
            wsprintf(szState, _T("%s"), _T("ESTABLISHED"));
            break;
        case MIB_TCP_STATE_FIN_WAIT1:
            wsprintf(szState, _T("%s"), _T("FIN_WAIT_1"));
            break;
        case MIB_TCP_STATE_FIN_WAIT2:
            wsprintf(szState, _T("%s"), _T("FIN_WAIT_2"));
            break;
        case MIB_TCP_STATE_CLOSE_WAIT:
            wsprintf(szState, _T("%s"), _T("CLOSE_WAIT"));
            break;
        case MIB_TCP_STATE_CLOSING:
            wsprintf(szState, _T("%s"), _T("CLOSING"));
            break;
        case MIB_TCP_STATE_LAST_ACK:
            wsprintf(szState, _T("%s"), _T("LAST_ACK"));
            break;
        case MIB_TCP_STATE_TIME_WAIT:
            wsprintf(szState, _T("%s"), _T("TIME_WAIT"));
            break;
        case MIB_TCP_STATE_DELETE_TCB:
            wsprintf(szState, _T("%s"), _T("DELETE_TCB"));
            break;
        }
        usLocalPort = ntohs((USHORT)pMibTcpTableOwnerPid->table[i].dwLocalPort);
        usRemotePort = _tcscmp(szState, _T("LISTENING")) == 0 ? 0 : ntohs((USHORT)pMibTcpTableOwnerPid->table[i].dwRemotePort);
        wsprintf(szLocal, _T("%s:%d"), szLocalAddr, usLocalPort);
        wsprintf(szRemote, _T("%s:%d"), szRemoteAddr, usRemotePort);
        wsprintf(szBuffer, _T("  %-7s%-23s%-23s%-16s%d\n"), _T("TCP"), szLocal, szRemote, szState, pMibTcpTableOwnerPid->table[i].dwOwningPid);
        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuffer, (DWORD)_tcslen(szBuffer), &dwWriteNum, NULL);
    }

    free(pMibTcpTableOwnerPid);
    FreeLibrary(hModule);
    return TRUE;
}

BOOL printUdp()
{
    int iErrno;
    PMIB_UDPTABLE_OWNER_PID pMibUdpTableOwnerPid;
    DWORD dwSize = 0;
    TCHAR szBuffer[1024];
    int i;
    HMODULE hModule;
    PFN_GET_EXTENDED_UDP_TABLE GetExtendedUdpTable;

    hModule = LoadLibrary(_T("iphlpapi.dll"));
    GetExtendedUdpTable = (PFN_GET_EXTENDED_UDP_TABLE)GetProcAddress(hModule, "GetExtendedUdpTable");

    if ((iErrno = GetExtendedUdpTable(NULL, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0)) != NO_ERROR)
    {
        if (iErrno != ERROR_INSUFFICIENT_BUFFER)
        {
            wsprintf(szBuffer, _T("GetExtendedUdpTable Error: %d\n"), iErrno);
            OutputDebugString(szBuffer);
            return FALSE;
        }
    }
    pMibUdpTableOwnerPid = (PMIB_UDPTABLE_OWNER_PID)malloc(dwSize);
    if (pMibUdpTableOwnerPid == NULL)
    {
        OutputDebugString(_T("malloc Error!"));
        return FALSE;
    }
    if ((iErrno = GetExtendedUdpTable(pMibUdpTableOwnerPid, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0)) != NO_ERROR)
    {
        wsprintf(szBuffer, _T("GetExtendedUdpTable Error: %d\n"), iErrno);
        OutputDebugString(szBuffer);
        return FALSE;
    }

    for (i = 0; i < (int)pMibUdpTableOwnerPid->dwNumEntries; i++)
    {
        IN_ADDR localAddr;
        TCHAR szLocalAddr[1024];
        USHORT usLocalPort;
        TCHAR szLocal[1024];
        TCHAR szRemote[1024];
        TCHAR szState[1024];
        DWORD dwWriteNum;

        localAddr.S_un.S_addr = pMibUdpTableOwnerPid->table[i].dwLocalAddr;
        usLocalPort = ntohs((USHORT)pMibUdpTableOwnerPid->table[i].dwLocalPort);
        MultiByteToWideChar(CP_ACP, 0, inet_ntoa(localAddr), -1, szLocalAddr, 1024);
        wsprintf(szLocal, _T("%s:%d"), szLocalAddr, usLocalPort);
        wsprintf(szRemote, _T("*:*"));
        wsprintf(szState, _T(""));
        wsprintf(szBuffer, _T("  %-7s%-23s%-23s%-16s%d\n"), _T("UDP"), szLocal, szRemote, szState, pMibUdpTableOwnerPid->table[i].dwOwningPid);
        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuffer, (DWORD)_tcslen(szBuffer), &dwWriteNum, NULL);
    }

    free(pMibUdpTableOwnerPid);
    FreeLibrary(hModule);
    return TRUE;
}

int _tmain()
{
    TCHAR szBuffer[1024];
    DWORD dwWriteNum;

    wsprintf(szBuffer, _T("  %-7s%-23s%-23s%-16s%-s\n"), _T("Proto"), _T("Local Address"), _T("Foreign Address"), _T("State"), _T("PID"));
    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuffer, (DWORD)_tcslen(szBuffer), &dwWriteNum, NULL);

    printTcp();
    printUdp();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值