|   //非正常回复if (icmphdr->type != ICMP_TTL_EXPIRE)
 {
 //ttl减为零
 if (icmphdr->type == ICMP_DEST_UNREACH)
 {
 //主机不可达
 cerr << "Destination unreachable" << endl;
 }
 else
 {
 //非法的ICMP包类型
 cerr << "Unknown ICMP packet type " << int(icmphdr->type) <<" received" << endl;
 }
 return - 1;
 }
 }
 else if (icmphdr->id != (unsigned short)GetCurrentProcessId())
 {
 //不是本进程发的包, 可能是同机的其它ping进程发的
 return - 2;
 }
 
 // 指出往返时间TTL
 int nHops = int(256-reply->ttl);
 if (nHops == 192)
 {
 // TTL came back 64, so ping was probably to a host on the
 // LAN -- call it a single hop.
 nHops = 1;
 }
 else if (nHops == 128)
 {
 // Probably localhost
 nHops = 0;
 }
  // 输出信息cout << endl << bytes << " bytes from " << inet_ntoa(from->sin_addr) <<", icmp_seq " << icmphdr->seq << ", ";
 if (icmphdr->type == ICMP_TTL_EXPIRE)
 {
 cout << "TTL expired." << endl;
 }
 else
 {
 cout << nHops << " hop" << (nHops == 1 ? "" : "s");
 cout << ", time: " << (GetTickCount() - icmphdr->timestamp) << " ms." <<endl;
 }
 return 0;
 }
   为了在Visual C++中更加方便地使用发送和接收ICMP报文,我们可以使用由Jay Wheeler编写的CIcmp(An ICMP Class For MFC)类,在著名的开发网站的如下地址可以下载。   这个类的简要框架如下: 
class CIcmp: public CSocket{
 // Attributes
 public:
 BOOL OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents);
 BOOL OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents, int AFamily, int AType, int AProtocol);
 int CloseIcmpSocket(void);
 BOOL Connect(int ReceiveTimeout, int SendTimeout);
 BOOL Connect(LPINT ReceiveTimeout, LPINT SendTimeout, int AFamily, int AType, int AProtocol);
 int SetTTL(int TTL);
 int SetAsynchNotification(HWND hWnd, unsigned int Message, long Events);
 int Receive(LPSTR pIcmpBuffer, int IcmpBufferSize);
 unsigned long GetIPAddress(LPSTR iHostName);
 int Ping(LPSTR pIcmpBuffer, int IcmpBufferSize);
 unsigned short IcmpChecksum(unsigned short FAR *lpBuf, int Len);
 void DisplayError(CString ErrorType, CString FunctionName);
 
		      
		      
		      
		      
		      
		      
                        共9页: 上一页 [1] [2] [3] [4] [5] 6 [7] [8] [9] 下一页 |