VC Ping IP class

Source: Internet
Author: User
Pingip. cpp

# Include "stdafx. H "# include" Ping. h "void cping: Ping (uint nretries, maid, hwnd) {socket rawsocket; lphostent lphost; uint nloop; int nret; struct sockaddr_in sadest; struct sockaddr_in sasrc; DWORD dwtimesent; DWORD dwelapsed; u_char cttl; m_hwnd = hwnd; cstring STR; // create a raw socket rawsocket = socket (af_inet, sock_raw, ipproto_icmp); If (rawsocket = socket_error) {cstring strmsg; strmsg. format ("An error occurred while creating a socket Error-wsaerror: % LD ", wsagetlasterror (); // send the error message sendmessage (m_hwnd, wm_msg_status, 0, (lparam) allocbuffer (strmsg); return ;} // obtain the host information lphost = gethostbyname (pstrhost); // construct the target socket address information sadest. sin_addr.s_addr = * (u_long far *) (lphost-> h_addr); sadest. sin_family = af_inet; sadest. sin_port = 70; // pingfor (nloop = 0; nloop <nretries; nloop ++) {// send the ICMP Response Request sendechorequest (rawsocket, & sadest); nret = waitforechorepl Y (rawsocket); If (! Nret) {Str. format ("request timed out"); sendmessage (m_hwnd, wm_msg_status, 3, (lparam) allocbuffer (STR);} else {// get response dwtimesent = recvechoreply (rawsocket, & sasrc, & cttl); // calculation time: dwelapsed = gettickcount ()-dwtimesent; Str. format ("reply from: % s: bytes = % d time = % ldms TTL = % d", inet_ntoa (sasrc. sin_addr), req_datasize, dwelapsed, cttl); sendmessage (m_hwnd, wm_msg_status, 2, (lparam) allocbuffer (STR); sleep (1000);} nret = Closesocket (rawsocket); If (nret = socket_error) {cstring strmsg; strmsg. format ("socket disabling error-wsaerror: % lD", wsagetlasterror (); // send an error message sendmessage (m_hwnd, wm_msg_status, 0, (lparam) allocbuffer (strmsg) ;}// sends an icmpecho packet request int cping: sendechorequest (socket S, lpsockaddr_in lpsttoaddr) {static echorequest echo; static nid = 1; static nseq = 1; int nret; // construct Response Request echo. icmphdr. type = 8; echo. icmphdr. code = 0; echo. Icmphdr. checksum = 0; echo. icmphdr. id = NID ++; echo. icmphdr. SEQ = nseq ++; For (nret = 0; nret <req_datasize; nret ++) echo. CDATA [nret] = ''+ nret; // Save the sending time ECHO. dwtime = gettickcount (); echo. icmphdr. checksum = in_cksum (u_short *) & Echo, sizeof (echorequest); // send the request nret = sendto (S, (lpstr) & Echo, sizeof (echorequest), 0, (lpsockaddr) lpsttoaddr, sizeof (sockaddr_in); If (nret = socket_error) {cstring strmsg; strmsg. format (" An error occurred while sending data-wsaerror: % LD ", wsagetlasterror (); // send an error message sendmessage (m_hwnd, wm_msg_status, 0, (lparam) allocbuffer (strmsg ));} return (nret);} // receives the icmpecho packet response DWORD cping: recvechoreply (socket S, lpsockaddr_in lpsafrom, u_char * pttl) {ECHOREPLY; int nret; int naddrlen = sizeof (struct sockaddr_in); // receives the request response nret = recvfrom (S, (lpstr) & ECHOREPLY, sizeof (ECHOREPLY), 0, (lpsockaddr) lpsafrom, & naddrlen); // check the returned value I F (nret = socket_error) {cstring strmsg; strmsg. format ("An error occurred while receiving data-wsaerror: % lD", wsagetlasterror (); // send an error message sendmessage (m_hwnd, wm_msg_status, 0, (lparam) allocbuffer (strmsg);} // returns the sending time * pttl = ECHOREPLY. iPhone dr. TTL; Return (ECHOREPLY. echorequest. dwtime) ;}// wait for response int cping: waitforechoreply (socket s) {struct timeval time; fd_set FDS; FDS. fd_count = 1; FDS. fd_array [0] = s; time. TV _sec = 1; time. TV _usec = 0; retur N (select (1, & FDS, null, null, & time) ;}// conversion address u_short cping: in_cksum (u_short * ADDR, int Len) {register int nleft = Len; register u_short * n = ADDR; register u_short answer; register int sum = 0; while (nleft> 1) {sum + = * n ++; nleft-= 2;} If (nleft = 1) {u_shortu = 0; * (u_char *) (& U) = * (u_char *) N; sum + = u;} sum = (sum> 16) + (sum & 0 xFFFF); sum + = (sum> 16); answer = ~ SUM; Return (answer);} Char * cping: allocbuffer (cstring strmsg) {int nlen = strmsg. getlength (); char * pbuffer = new char [nlen + 1]; strcpy (pbuffer, (const char *) strmsg); Assert (pbuffer! = NULL); Return pbuffer ;}

Pingip. h

// Ping. h // # define REQ_DATASIZE 32 // the size of the Echo request data class CPing {public: static char * AllocBuffer (CString strMsg); HWND m_hWnd; // window handle void Ping (UINT nRetries, LPCSTR pstrHost, HWND hWnd); int WaitForEchoReply (SOCKET s); // The intSendEchoRequest (SOCKET, LPSOCKADDR_IN) function of ICMP response ); DWORD sequence (SOCKET, LPSOCKADDR_IN, u_char * pTTL); u_short in_cksum (u_short * addr, int len); protected :}; typedef struct IP_HDR {u_char VIHL; // Version and sequence; // Type Of ServiceshortTotLen; // The total length Of shortID; // the flag Of lifecycle flagoff; // mark u_charTTL; // The Life Cycle u_charProtocol; // protocol u_1_checksum; // check and structin_addr iaSrc; // source address structin_addr iaDst; // Destination Address} IPHDR, * PIPHDR; typedef struct IC_MPHDR {u_charType; // type u_charCode; // encode u_1_checksum; // check and u_shortID; // identifies u_shortSeq; // sequential charData; // data} ICMPHDR, * PICMPHDR; typedef struct records {ICMPHDR icmpHdr; DWORDdwTime; charcData [REQ_DATASIZE];} ECHOREQUEST, * average; typedef struct ICMPECHOREPLY {IPHDRiphdr; ECHOREQUESTechorequest; char cFiller [256];} ECHOREPLY, * PECHOREPLY;

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.