Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14497352
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:53:48

下载本文示例代码
  以下的这个类是解决一个很普通的问题的:在一台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;   }   以下的这个类是解决一个很普通的问题的:在一台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;   } 下载本文示例代码


使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机使用TCP堆栈来Ping计算机
阅读(130) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~