C + + Implementation Ping Program instance _c language

Source: Internet
Author: User

This article illustrates the method of implementing Ping Program in C + +. Share to everyone for your reference. The implementation methods are as follows:

This instance involves sending and echoing ICMP packets, and the ping code is as follows:

Copy Code code as follows:
DWORD WINAPI ThreadProc (LPVOID lParam)
{
Cinitsock Initsock;

HWND hwnd = (HWND) LParam; Get handle from argument
Char szip[64] ={0};
:: Getdlgitemtexta (HWnd, idc_ip, Szip, sizeof (Szip)); To get an IP address from a control
1. To create an original socket with a protocol type of IPPROTO_ICMP, set socket properties
Socket Sraw =:: Socket (af_inet, Sock_raw, ipproto_icmp);
SetTimeout (Sraw, 1000, TRUE);

2. Create and initialize ICMP packets
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;
Populating data
memset (&buff[sizeof (ICMP_HDR)], ' E ', 32);

Start sending and receiving packets
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. Calling SendTo to send an ICMP request
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. Call Recvfrom Accept ICMP response
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;
}
Parsing received ICMP packets
int Ntick =:: GetTickCount ();
if (Nret < sizeof (ICMP_HDR) +sizeof (Ipheader))
{
Strtoshow + = "less byte recved." \ r \ n ";
}
icmp_hdr* precvicmp = (icmp_hdr*) (pszrevbuf + sizeof (ipheader));//Skip IP header
if (precvicmp->icmp_type!= 0)
{
Strtoshow + = "not echo type";
return-1;
}
if (precvicmp->icmp_id!=:: GetCurrentProcessId ())
{
Strtoshow + + "is not the echo of this process \ r \ n";
return-1;
}
CString strtemp;
Strtemp.format ("Received from [%s]ip%04d bytes \ r \ n", Inet_ntoa (FROM.SIN_ADDR), nret);
Strtoshow + = strtemp;

strtemp = "";
Strtemp.format ("Serial Number:%d\r\n", precvicmp->icmp_sequence);
Strtoshow + = strtemp;

strtemp = "";
Strtemp.format ("Spend time:%d\r\n", Ntick-precvicmp->icmp_timestamp);
}
Strtoshow + = "**********************************";
:: Setdlgitemtexta (HWnd, Idc_data, strtoshow);
return 0;
}

The IP header and ICMP header code are as follows:

Copy Code code as follows:
USHORT Checksum (ushort* buff, int nsize)//222.89.166.13
{
unsigned long cksum=0;
Add the data to the cksum in Word units
while (Nsize > 1)
{
Cksum + = *buff++;
Nsize-= sizeof (USHORT);
}

If it is odd, extend the last word to two words, then add
if (nsize)
{
Cksum + = * (uchar*) buff;
}
Add the high 16 bits of the cksum to the lower 16 bits, and get the checksum after the inverse
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;
The following is the Echo header
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;

I hope this article will help you with the C + + program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.