时间:2021-05-20
本文实例讲述了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++程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C++网络连通性检测的实现方法有时程序需要保持和某个指定网络的连通,有人的情况下我们直接用ping命令查看即可,然而程序自身检测会稍微麻烦一点。实现思路如下:用
本文实例为大家分享了C++实现走迷宫小游戏的具体代码,供大家参考,具体内容如下源码下载:C++实现走迷宫小游戏主程序代码:#include#include#in
本文实例讲述了C++编写DLL动态链接库的步骤与实现方法。分享给大家供大家参考,具体如下:在写C++程序时,时常需要将一个class写成DLL,供客户端程序调用
C++发邮件用的是阻塞式socket模型,发送完数据后需要接收返回值,才能接着发送。本程序不发送邮件附件,发附件的实例:C++实现含附件的邮件发送功能#incl
本文详细叙述了C/C++的位操作,这是C/C++程序设计中非常重要的概念。本文实例对于C/C++初学者来说也具有很好的复习与参考价值。具体分析如下:C/C++对