首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Visual C++ > 使用TCP堆栈来Ping计算机
【标  题】:使用TCP堆栈来Ping计算机
【关键字】:C,计算机,TC,in,TCP,Ping,TCP,Ping
【来  源】:网络

使用TCP堆栈来Ping计算机

//原著:Les Jordan
//译者:重庆大学光电工程学院 贾旭滨
//欢迎大家批评指教,谢谢!

以下的这个类是解决一个很普通的问题的:在一台WIN95的计算机上怎么样利用MSTCP堆栈去PING另外一台计算机。当然,这个类在NT3.51和NT4上也可以用。显然,MicroSoft公司不会那么笨,在WIN系统中又另外构造这么一个单独的机制来解决这个问题,让本来就复杂的WIN系统更加复杂。那么,我们只能用ICMP DLL自己来解决这个问题了。不过,很让人失望,MicroSoft公司直到Winsock 2.0也没有解决这个问题。

难题就是:给一个计算机的名字,或者一台计算机的IP地址,怎么样去PING它,而且得到它的响应时间。所以我们用了ICMP DLL,而对于一些SOCKET结构来说,在CSocket中已经全部都有定义了。所以,下面的类是一个CSocket的子类,它将会有更好的功能来解决更多的问题。不过你得先得到ICMPAPI.H,ICMAPI.CPP,ICMP.LIB和IPEXPORT.H,IPEXPORT.CPP这些文件,这些文件应该加在你的工程中。这些文件你可以在Microsoft Developers Network的光盘上得到,不过在微软公司的主页上也能拿到,而且更新。

类中有4个公共函数,如下:
unsigned long ResolveIP(CString strIP)
unsigned long ResolveName(CString strHostName)
CString GetIP(unsigned long ulIP)
DWORD PingHost(unsigned long ulIP, int iPingTimeout)

ResolveIP(CString strIP)函数使用一个IP地址的字符串来作为参数,返回值是IP地址值。
ResolveName(CString strHostName)函数使用一计算机名的字符串来作为参数,经过DNS或者WINS的解析,返回值是被PING计算机的IP 地址,注意它使用了GetHostByName模块化。

GetIP(unsigned long ulIP)函数是以IP地址作为参数(注意是IP地址),返回值是一个表示IP地址的字符串。

PingHost(unsigned long ulIP, int iPingTimeout)函数,第1个参数是IP地址(注意是IP地址,而不是IP地址的字符串),第2个参数是表示时间值的,表示间隔时间的。函数功能是,去PING一台计算机,返回值是PING的响应时间。

以下是代码:

//*
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//icmpecho.h
//-------------------------------------------------------------------------
//------------------------------------------------------------------------
//*
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 "IcmpEcho.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;

}
如何得到多穴主机的多个IP地址:【上一篇】
常用算法与数据结构(二):【下一篇】
【相关文章】
  • 巧用StretchBlt实现图像放大镜
  • WinAMP插件DIY
  • 编写自己的CD播放器
  • DirectShow系统初级指南
  • 在ActiveX控件中检测IE中STOP按钮的按下
  • ActiveX技术综述
  • 编写易于调试的VC代码
  • 亲密接触VC6.0编译器
  • VC++ Studio使用技巧
  • 用VC修改目录的日期和时间
  • 【随机文章】
  • 3DS Max 7.0 PF Source粒子全攻略(28)
  • windows核心编程--线程高级
  • 18届国际混乱C程序大赛开幕啦
  • 14.4.1 Argument lists
  • 一个sql的执行步骤
  • 建立可伸缩的营销平台——标准产品业务模式CRM应用方案
  • Linux Kernel Development读书笔记一(Chapter3 Process)
  • OOD原则:SRP、OCP以及LSP
  • Authorware6.5轻松学:六、丰富的扩展函数插件
  • 说C语言死了的人是猪
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.