18,356
社区成员
发帖
与我相关
我的任务
分享
//------------------------------------------------------------------------------------------------------------------
//icmpecho.h
//------------------------------------------------------------------------------------------------------------------
#include <afxsock.h>
#pragma comment(lib, "Iphlpapi.lib")
class CIcmpEcho : public CSocket
{
// Attributes
public:
// Operations
public:
CIcmpEcho();
virtual ~CIcmpEcho();
unsigned long ResolveIP(CString strIP);
unsigned long ResolveName(CString strHostName);
DWORD PingHost(unsigned long ulIP, int iPingTimeout);
CString GetIP(unsigned long ulIP);
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIcmpEcho)
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CIcmpEcho)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
protected:
};
/////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------------------------------------------
//icmpecho.cpp
//------------------------------------------------------------------------------------------------------------------
// IcmpEcho.cpp : implementation file
//
#include "CIcmpEcho.h"
extern "C" {
#include "ipexport.h"
#include "icmpapi.h"
}
#define PING_TIMEOUT 100
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CIcmpEcho
CIcmpEcho::CIcmpEcho()
{
}
CIcmpEcho::~CIcmpEcho()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CIcmpEcho, CSocket)
//{{AFX_MSG_MAP(CIcmpEcho)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CIcmpEcho member functions
unsigned long CIcmpEcho::ResolveIP(CString strIP)
{
//Task 1: Given IP Address i.e. "111.111.111.111",
// Return Network byte ordered address (ulIP)
unsigned long ulIP;
ulIP =(IPAddr)inet_addr(strIP);
return ulIP;
}
unsigned long CIcmpEcho::ResolveName(CString strHostName)
{
//Task 1: Resolve HostName (through DNS or WINS, whichever appropriate)
//Task 2: Return network byte ordered address (ulIP)
unsigned long ulIP;
hostent* phostent;
phostent = gethostbyname(strHostName);
if (phostent == NULL)
return 0;
ulIP = *(DWORD*)(*phostent->h_addr_list);
return ulIP;
}
DWORD CIcmpEcho::PingHost(unsigned long ulIP, int iPingTimeout)
{
//Task 1: Open ICMP Handle
//Task 2: Create Structure to receive ping reply
//Task 3: SendPing (SendEcho)
//Task 4: Close ICMP Handle
//Task 5: Return RoundTripTime
unsigned long ip = ulIP;
HANDLE icmphandle = IcmpCreateFile();
char reply[sizeof(icmp_echo_reply)+8];
icmp_echo_reply* iep=(icmp_echo_reply*)&reply;
iep->RoundTripTime = 0xffffffff;
IcmpSendEcho(icmphandle,ip,0,0,NULL,reply,sizeof(icmp_echo_reply)+8,iPingTimeout);
IcmpCloseHandle(icmphandle);
return iep->RoundTripTime;
}
CString CIcmpEcho::GetIP(unsigned long ulIP)
{
//Task 1: Given a host order ulIP Address
// Return a IP address in format of xxx.xxx.xxx.xxx
LPSTR szAddr;
struct in_addr inetAddr;
inetAddr.s_addr = (IPAddr)ulIP;
szAddr = inet_ntoa(inetAddr);
CString csIP = szAddr;
return csIP;
}
///////////////////
//使用部分
CIcmpEcho icmp;
unsigned long lIP = icmp.ResolveIP("192.168.1.1");
DWORD dw = icmp.PingHost(lIP, 1000);
try
{
socket.Connect(iep);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
return true;
}
catch ( SocketException ex)
{
//目标主机无应答时
if (ex.ErrorCode==10060)
{
socket.Close();
return false;
}
return true;
}