怎样检测主机上的端口是否开放

时间:2021-08-16 15:22:58
我想写一个程序,隔一段时间去扫描一下几台主机的端口是否工作正常(比如80端口、1433端口等),不知用何控件比较简单实现。有源代码就更好啦

9 个解决方案

#1


http://download.csdn.net/source/1625991

这里有个现成的 嘿嘿。

#2


//---------------------------------------------------------------------------
#ifndef uGetAllListH
#define uGetAllListH

#include "iphlpapi.h"
#include "winsock.hpp"

#pragma link "C:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\iphlpapi.lib"

//---------------------------------------------------------------------------
String strTcpState[] =
{
    "未知状态", "已经关闭", "监听中", "同步发送",
    "同步接收", "已建立", "FIN_WAIT", "FIN_WAIT2",
    "等待关闭", "正在关闭", "LAST_ACK", "超时", "DELETE_TCB"
};
static PMIB_TCPTABLE pTcpTable;
static PMIB_UDPTABLE pUdpTable;
//---------------------------------------------------------------------------
// 解析IP,根据IP地址获取主机名称
String __fastcall GetHost(UINT unIpAddr)
{
    PHostEnt pHostEnt;
    TInAddr iaInAddr;
    String strRet;
    if(unIpAddr == 0)
        strRet = "0.0.0.0";
    else
    {
        Application->ProcessMessages();
        iaInAddr.S_un.S_addr = unIpAddr;
        strRet = strRet.sprintf("%d.%d.%d.%d",
                BYTE(iaInAddr.S_un.S_un_b.s_b1),
                BYTE(iaInAddr.S_un.S_un_b.s_b2),
                BYTE(iaInAddr.S_un.S_un_b.s_b3),
                BYTE(iaInAddr.S_un.S_un_b.s_b4));
    }
    return strRet;
}
//---------------------------------------------------------------------------
// 根据连接类型,获取端口的描述
String __fastcall GetPort(UINT unPort, char *pcProto)
{
    PServEnt pServEnt;
    Application->ProcessMessages();
    pServEnt = getservbyport(htons(unPort), pcProto);
    if(pServEnt != NULL)
        return String(unPort) + "(" + String(pServEnt->s_name) + ")";
    else
        return String(IntToStr(unPort));
};
//---------------------------------------------------------------------------
// 获取TCP链接的信息
void __fastcall MyGetTcpTable(TListView *lv)
{
    DWORD dwRetVal;
    DWORD dwLocalPort, dwLocalAddr, dwRemotePort, dwRemoteAddr;

    if(pTcpTable != NULL)
        GlobalFree(pTcpTable);
    pTcpTable = (MIB_TCPTABLE *)GlobalAlloc(GMEM_SHARE, sizeof(MIB_TCPTABLE));
    DWORD dwSize = 0;

    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
    if(GetTcpTable(pTcpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)
    {
        GlobalFree(pTcpTable);
        pTcpTable = (MIB_TCPTABLE *)GlobalAlloc(GMEM_SHARE, (UINT)dwSize);
    }

    // Make a second call to GetTcpTable to get
    // the actual data we require
    dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE);
    if(dwRetVal== NO_ERROR)
    {
        for(int i = 0; i < (int) pTcpTable->dwNumEntries; i++)
        {
            dwLocalPort = pTcpTable->table[i].dwLocalPort / 256 +
                    (pTcpTable->table[i].dwLocalPort % 256) * 256;
            dwLocalAddr = pTcpTable->table[i].dwLocalAddr;
            dwRemotePort = pTcpTable->table[i].dwRemotePort / 256 +
                    (pTcpTable->table[i].dwRemotePort % 256) * 256;
            dwRemoteAddr = pTcpTable->table[i].dwRemoteAddr;
            TListItem *pItem = lv->Items->Add();
            pItem->Caption = String(i + 1);
            pItem->SubItems->Add("TCP");
            pItem->SubItems->Add(GetHost(dwLocalAddr) + ":" + String(dwLocalPort));
            pItem->SubItems->Add(GetHost(dwRemoteAddr) + ":" + String(dwRemotePort));
            pItem->SubItems->Add(strTcpState[pTcpTable->table[i].dwState]);
            pItem->ImageIndex = pTcpTable->table[i].dwState == 11 ? 1: 0;
            pItem->SubItems->Add("本地端口:" + GetPort(dwLocalPort, "TCP") +
                    "  远程端口:" + GetPort(dwRemotePort, "TCP"));
        }
    }
    else
    {
        MessageBox(Application->Handle, SysErrorMessage(GetLastError()).c_str() ,
            Application->Title.c_str(), MB_OK | MB_ICONERROR);
    }
}
//---------------------------------------------------------------------------
// 获取UDP连接的信息
void __fastcall MyGetUdpTable(TListView *lv)
{
    DWORD dwRetVal;
    DWORD dwLocalPort, dwLocalAddr;
    if(pUdpTable != NULL)
        GlobalFree(pUdpTable);
    pUdpTable = (MIB_UDPTABLE *)GlobalAlloc(GMEM_SHARE, sizeof(MIB_UDPTABLE));
    DWORD dwSize = 0;

    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
    if(GetUdpTable(pUdpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)
    {
        GlobalFree(pUdpTable);
        pUdpTable = (MIB_UDPTABLE *)GlobalAlloc(GMEM_SHARE, (UINT)dwSize);
    }

    // Make a second call to GetUdpTable to get
    // the actual data we require
    dwRetVal = GetUdpTable(pUdpTable, &dwSize, TRUE);
    if(dwRetVal == NO_ERROR)
    {
        for(int i = 0; i < (int)pUdpTable->dwNumEntries; i++)
        {
            dwLocalPort = pUdpTable->table[i].dwLocalPort / 256 +
                    (pUdpTable->table[i].dwLocalPort % 256) * 256;
            dwLocalAddr = pUdpTable->table[i].dwLocalAddr;
            TListItem *pItem = lv->Items->Add();
            pItem->Caption = String(lv->Items->Count);
            pItem->SubItems->Add("UDP");
            pItem->SubItems->Add(GetHost(dwLocalAddr) + ":" + String(dwLocalPort));
            pItem->SubItems->Add("*.*.*.*:*");
            pItem->SubItems->Add(strTcpState[pTcpTable->table[i].dwState]);
            pItem->ImageIndex = 2;
            pItem->SubItems->Add("本地端口:" + GetPort(dwLocalPort, "UDP"));
        }
    }
    else
    {
        MessageBox(Application->Handle, SysErrorMessage(GetLastError()).c_str() ,
            Application->Title.c_str(), MB_OK | MB_ICONERROR);
    }
}
//---------------------------------------------------------------------------
// 删除链接
bool __fastcall MyDeleteConnection(int nIndex)
{
    pTcpTable->table[nIndex].dwState = MIB_TCP_STATE_DELETE_TCB;
    DWORD dwResult = SetTcpEntry(&pTcpTable->table[nIndex]);
    if(dwResult == NO_ERROR)
    {
        MessageBox(Application->Handle, "删除连接成功!",
                Application->Title.c_str(), MB_OK | MB_ICONINFORMATION);
        return true;
    }
    else
    {
        MessageBox(Application->Handle, "删除连接失败,该状态的连接不可删除!",
                Application->Title.c_str(), MB_OK | MB_ICONERROR);
        return false;
    }
}

#3


与目标机器的端口尝试连接即可。如果连接失败,则认为是该端口处于关闭状态。

#4


另一方法可直接调用系统的
netstat -na
将结果写入文件或重定向到你的控件中

#5


学习









.

#6


收藏,学习

#7


收藏,学习

#8


扫描端口的方法可多了,针对TCP和UDP端口的方式也有不同之处,针对Windows和UNIX操作系统的端口扫描技术也有不同。

#9


学习下

#1


http://download.csdn.net/source/1625991

这里有个现成的 嘿嘿。

#2


//---------------------------------------------------------------------------
#ifndef uGetAllListH
#define uGetAllListH

#include "iphlpapi.h"
#include "winsock.hpp"

#pragma link "C:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\iphlpapi.lib"

//---------------------------------------------------------------------------
String strTcpState[] =
{
    "未知状态", "已经关闭", "监听中", "同步发送",
    "同步接收", "已建立", "FIN_WAIT", "FIN_WAIT2",
    "等待关闭", "正在关闭", "LAST_ACK", "超时", "DELETE_TCB"
};
static PMIB_TCPTABLE pTcpTable;
static PMIB_UDPTABLE pUdpTable;
//---------------------------------------------------------------------------
// 解析IP,根据IP地址获取主机名称
String __fastcall GetHost(UINT unIpAddr)
{
    PHostEnt pHostEnt;
    TInAddr iaInAddr;
    String strRet;
    if(unIpAddr == 0)
        strRet = "0.0.0.0";
    else
    {
        Application->ProcessMessages();
        iaInAddr.S_un.S_addr = unIpAddr;
        strRet = strRet.sprintf("%d.%d.%d.%d",
                BYTE(iaInAddr.S_un.S_un_b.s_b1),
                BYTE(iaInAddr.S_un.S_un_b.s_b2),
                BYTE(iaInAddr.S_un.S_un_b.s_b3),
                BYTE(iaInAddr.S_un.S_un_b.s_b4));
    }
    return strRet;
}
//---------------------------------------------------------------------------
// 根据连接类型,获取端口的描述
String __fastcall GetPort(UINT unPort, char *pcProto)
{
    PServEnt pServEnt;
    Application->ProcessMessages();
    pServEnt = getservbyport(htons(unPort), pcProto);
    if(pServEnt != NULL)
        return String(unPort) + "(" + String(pServEnt->s_name) + ")";
    else
        return String(IntToStr(unPort));
};
//---------------------------------------------------------------------------
// 获取TCP链接的信息
void __fastcall MyGetTcpTable(TListView *lv)
{
    DWORD dwRetVal;
    DWORD dwLocalPort, dwLocalAddr, dwRemotePort, dwRemoteAddr;

    if(pTcpTable != NULL)
        GlobalFree(pTcpTable);
    pTcpTable = (MIB_TCPTABLE *)GlobalAlloc(GMEM_SHARE, sizeof(MIB_TCPTABLE));
    DWORD dwSize = 0;

    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
    if(GetTcpTable(pTcpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)
    {
        GlobalFree(pTcpTable);
        pTcpTable = (MIB_TCPTABLE *)GlobalAlloc(GMEM_SHARE, (UINT)dwSize);
    }

    // Make a second call to GetTcpTable to get
    // the actual data we require
    dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE);
    if(dwRetVal== NO_ERROR)
    {
        for(int i = 0; i < (int) pTcpTable->dwNumEntries; i++)
        {
            dwLocalPort = pTcpTable->table[i].dwLocalPort / 256 +
                    (pTcpTable->table[i].dwLocalPort % 256) * 256;
            dwLocalAddr = pTcpTable->table[i].dwLocalAddr;
            dwRemotePort = pTcpTable->table[i].dwRemotePort / 256 +
                    (pTcpTable->table[i].dwRemotePort % 256) * 256;
            dwRemoteAddr = pTcpTable->table[i].dwRemoteAddr;
            TListItem *pItem = lv->Items->Add();
            pItem->Caption = String(i + 1);
            pItem->SubItems->Add("TCP");
            pItem->SubItems->Add(GetHost(dwLocalAddr) + ":" + String(dwLocalPort));
            pItem->SubItems->Add(GetHost(dwRemoteAddr) + ":" + String(dwRemotePort));
            pItem->SubItems->Add(strTcpState[pTcpTable->table[i].dwState]);
            pItem->ImageIndex = pTcpTable->table[i].dwState == 11 ? 1: 0;
            pItem->SubItems->Add("本地端口:" + GetPort(dwLocalPort, "TCP") +
                    "  远程端口:" + GetPort(dwRemotePort, "TCP"));
        }
    }
    else
    {
        MessageBox(Application->Handle, SysErrorMessage(GetLastError()).c_str() ,
            Application->Title.c_str(), MB_OK | MB_ICONERROR);
    }
}
//---------------------------------------------------------------------------
// 获取UDP连接的信息
void __fastcall MyGetUdpTable(TListView *lv)
{
    DWORD dwRetVal;
    DWORD dwLocalPort, dwLocalAddr;
    if(pUdpTable != NULL)
        GlobalFree(pUdpTable);
    pUdpTable = (MIB_UDPTABLE *)GlobalAlloc(GMEM_SHARE, sizeof(MIB_UDPTABLE));
    DWORD dwSize = 0;

    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
    if(GetUdpTable(pUdpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)
    {
        GlobalFree(pUdpTable);
        pUdpTable = (MIB_UDPTABLE *)GlobalAlloc(GMEM_SHARE, (UINT)dwSize);
    }

    // Make a second call to GetUdpTable to get
    // the actual data we require
    dwRetVal = GetUdpTable(pUdpTable, &dwSize, TRUE);
    if(dwRetVal == NO_ERROR)
    {
        for(int i = 0; i < (int)pUdpTable->dwNumEntries; i++)
        {
            dwLocalPort = pUdpTable->table[i].dwLocalPort / 256 +
                    (pUdpTable->table[i].dwLocalPort % 256) * 256;
            dwLocalAddr = pUdpTable->table[i].dwLocalAddr;
            TListItem *pItem = lv->Items->Add();
            pItem->Caption = String(lv->Items->Count);
            pItem->SubItems->Add("UDP");
            pItem->SubItems->Add(GetHost(dwLocalAddr) + ":" + String(dwLocalPort));
            pItem->SubItems->Add("*.*.*.*:*");
            pItem->SubItems->Add(strTcpState[pTcpTable->table[i].dwState]);
            pItem->ImageIndex = 2;
            pItem->SubItems->Add("本地端口:" + GetPort(dwLocalPort, "UDP"));
        }
    }
    else
    {
        MessageBox(Application->Handle, SysErrorMessage(GetLastError()).c_str() ,
            Application->Title.c_str(), MB_OK | MB_ICONERROR);
    }
}
//---------------------------------------------------------------------------
// 删除链接
bool __fastcall MyDeleteConnection(int nIndex)
{
    pTcpTable->table[nIndex].dwState = MIB_TCP_STATE_DELETE_TCB;
    DWORD dwResult = SetTcpEntry(&pTcpTable->table[nIndex]);
    if(dwResult == NO_ERROR)
    {
        MessageBox(Application->Handle, "删除连接成功!",
                Application->Title.c_str(), MB_OK | MB_ICONINFORMATION);
        return true;
    }
    else
    {
        MessageBox(Application->Handle, "删除连接失败,该状态的连接不可删除!",
                Application->Title.c_str(), MB_OK | MB_ICONERROR);
        return false;
    }
}

#3


与目标机器的端口尝试连接即可。如果连接失败,则认为是该端口处于关闭状态。

#4


另一方法可直接调用系统的
netstat -na
将结果写入文件或重定向到你的控件中

#5


学习









.

#6


收藏,学习

#7


收藏,学习

#8


扫描端口的方法可多了,针对TCP和UDP端口的方式也有不同之处,针对Windows和UNIX操作系统的端口扫描技术也有不同。

#9


学习下