怎样检测主机上的端口是否开放
我想写一个程序,隔一段时间去扫描一下几台主机的端口是否工作正常(比如80端口、1433端口等),不知用何控件比较简单实现。有源代码就更好啦
[解决办法]
http://download.csdn.net/source/1625991
这里有个现成的 嘿嘿。
[解决办法]
//---------------------------------------
#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;
}
}