程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++完成ping法式實例

C++完成ping法式實例

編輯:關於C++

C++完成ping法式實例。本站提示廣大學習愛好者:(C++完成ping法式實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成ping法式實例正文


本文實例講述了C++完成ping法式的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

該實例觸及ICMP數據包的發送與回顯,PING法式代碼以下:
DWORD WINAPI ThreadProc(LPVOID lParam)
{
 CInitSock initSock;
 
 HWND hWnd = (HWND)lParam; //從參數獲得句柄
 char szIp[64] ={0};
 ::GetDlgItemTextA(hWnd, IDC_IP, szIp, sizeof(szIp)); //從控件獲得ip地址
 //1. 創立協定類型為IPPROTO_ICMP的原始套接字,設置套接字屬性
 SOCKET sRaw = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
 SetTimeOut(sRaw, 1000, TRUE);

 //2.創立並初始化ICMP封包
 char buff[sizeof(ICMP_HDR)+32] = {0};
 ICMP_HDR* pIcmp = (ICMP_HDR*)buff;
 pIcmp->icmp_type = 8;
 pIcmp->icmp_code = 0;
 pIcmp->icmp_checksum = 0;
 pIcmp->icmp_id = (USHORT)::GetCurrentProcessId();
 pIcmp->icmp_sequence = 0;
 pIcmp->icmp_timestamp = 0;
 //填湊數據
 memset(&buff[sizeof(ICMP_HDR)], 'E', 32);

 //開端發送和吸收封包
 USHORT nSeq = 0;
 SOCKADDR_IN dest;
 dest.sin_family = AF_INET;
 dest.sin_port = htons(0);
 dest.sin_addr.S_un.S_addr = inet_addr(szIp); //inet_addr("192.168.19.63"); //inet_addr(szIp);220.181.111.147
 CString strToShow="";
 char* pszRevBuf = new char[1024]; //[1024] = {0};
 memset(pszRevBuf, 0, 1024);
 SOCKADDR_IN from;
 int nFromLen= sizeof(from);
 while (TRUE)
 {
  //3. 挪用sendto發送ICMP要求
  static int nCount=0;
  if (nCount++==4)
  {
   break;
  }
  pIcmp->icmp_checksum = checksum((USHORT*)buff, sizeof(ICMP_HDR)+32);
  pIcmp->icmp_sequence = ++nSeq;
  pIcmp->icmp_timestamp = ::GetTickCount();
  int nRet;
  nRet = ::sendto(sRaw, buff, sizeof(ICMP_HDR)+32, 0, (SOCKADDR*)&dest, sizeof(dest));
  if (nRet == SOCKET_ERROR)
  {
   int nError = ::WSAGetLastError();
   strToShow += "sendto error\r\n";
   return -1;
  }
  
  //4. 挪用recvfrom接收ICMP呼應
  nRet = ::recvfrom(sRaw, pszRevBuf, 1024, 0, (sockaddr*)&from, &nFromLen);
  if (nRet == SOCKET_ERROR)
  {
   if (::WSAGetLastError() == WSAETIMEDOUT)
   {
    strToShow += "timeout\r\n";
    continue;
   }
   strToShow += "recvfrom error\r\n";
   return -1;
  }
  //解析吸收到的ICMP包
  int nTick = ::GetTickCount();
  if (nRet < sizeof(ICMP_HDR)+sizeof(IPHeader))
  {
   strToShow += "less byte recved..\r\n";
  }
  ICMP_HDR* pRecvIcmp = (ICMP_HDR*)(pszRevBuf + sizeof(IPHeader));// 跳過IP頭
  if (pRecvIcmp->icmp_type != 0)
  {
   strToShow += "不是回顯類型";
   return -1;
  }
  if (pRecvIcmp->icmp_id != ::GetCurrentProcessId())
  {
   strToShow += "不是本過程的回顯包\r\n";
   return -1;
  }
  CString strTemp;
  strTemp.Format("收到來自於[%s]IP的%04d字節\r\n", inet_ntoa(from.sin_addr), nRet);
  strToShow += strTemp;

  strTemp = "";
  strTemp.Format("序列號:%d\r\n", pRecvIcmp->icmp_sequence);
  strToShow += strTemp;

  strTemp = "";
  strTemp.Format("消費時光:%d\r\n", nTick - pRecvIcmp->icmp_timestamp);
 }
 strToShow += "**********************************";
 ::SetDlgItemTextA(hWnd, IDC_DATA, strToShow);
 return 0;
}

IP頭和ICMP頭代碼以下:
USHORT checksum(USHORT* buff, int nSize) //222.89.166.13
{
 unsigned long cksum=0;
 //將數據以字為單元加到cksum
 while (nSize > 1)
 {
  cksum += *buff++;
  nSize -= sizeof(USHORT);
 }

 //假如為奇數, 將最初一個字擴大到雙字,再累加
 if (nSize)
 {
  cksum += *(UCHAR*)buff;
 }
 //將cksum的高16位與低16位相加,取反後獲得校驗和
 cksum = (cksum>>16) + (cksum & 0xffff);
 cksum += (cksum>>16);
 return (USHORT)(~cksum);
}
BOOL SetTimeOut(SOCKET s, int nTime, BOOL bRecv)
{
 int ret = ::setsockopt(s, SOL_SOCKET, bRecv?SO_RCVTIMEO:SO_SNDTIMEO, (char*)&nTime, sizeof(nTime));
 return ret!=SOCKET_ERROR;
}
typedef struct icmp_hdr
{
 unsigned char icmp_type;
 unsigned char icmp_code;
 unsigned short icmp_checksum;
 //上面是回顯頭
 unsigned short icmp_id;
 unsigned short icmp_sequence;
 unsigned long icmp_timestamp;
}ICMP_HDR, *PICMP_HDR;

typedef struct _IPHeader
{
 UCHAR iphVerLen;
 UCHAR ipTOS;
 USHORT ipLength;
 USHORT ipID;
 USHORT ipFlag;
 UCHAR ipTTL;
 UCHAR ipProtocol;
 USHORT ipCheckSum;
 ULONG ipSource;
 ULONG ipDestination;
}IPHeader, *PIPHeader;

願望本文所述對年夜家的C++法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved