Ping Command implementation)

Source: Internet
Author: User

Ping (packet Internet groper) is a very useful command that can be used to test the connectivity between two hosts. ping uses ICMP send-back request and send-back response packets. ping is an example of using network layer ICMP directly at the application layer. it does not pass through TCP or UDP of the transport layer.

The simplest way to program ping on Windows is to call the dynamic link library of iphlpapi. dll and reference the following functions:

1. icmpcreatefile (); // opens a handle on which IPv4 ICMP echo requests can be issued.

2. icmpsendecho (); // sends an IPv4 ICMP echo request and returns any echo response replies.

// The call returns when the time-out has expired or the reply buffer is filled.

3. icmpclosehandle (); // closes a handle opened by a call to the icmpcreatefile or icmp6createfile functions.

The following is the implementationCode:

 

  // Ping. c   //   // Use ICMP send-back request and answer packet to simulate ping command   //   // Step:   // 1. Open ICMP handle   // 2. sends an IPv4 ICMP echo request and returns any echo response replies.   // 3. display the infomation for response echo.   // 4. Close ICMP handle   //   // Configuration before build solution:   // Project-> properties-> linker-> input-> addtional dependencies: iphlpapi. Lib ws2_32.lib # Include <winsock2.h> # include <iphlpapi. h> # include <icmpapi. h> # include <stdio. h> Int Main ( Int Argc, Char ** Argv ){ /* Declare and initialize the variable */ Handle hicmp = invalid_handle_value; unsigned Long Uldestipaddr = inaddr_none; Char Sendbuffer [] = "Send buffer" ; Lpvoid lpreplybuffer = NULL; Int Ireplysize = 0; DWORD dwtimeout = 1000; DWORD dwretval = 0; /* Open the ICMP handle */ Hicmp = icmpcreatefile ();If (Invalid_handle_value = hicmp) {printf ( "Open ICMP failure! \ N" ); Return Exit_failure ;} /* Verify the command line parameters */       If (Argc! = 2) {printf ( "Usage: % s IP address \ n" , Argv [0]); icmpclosehandle (hicmp ); Return Exit_failure;} uldestipaddr = inet_addr (argv [1]); If (* Argv [1] = inaddr_none) {printf ( "Usage: % s IP address \ n" , Argv [0]); icmpclosehandle (hicmp ); Return Exit_failure;} ireplysize = Sizeof (Icmp_echo_reply) + Sizeof (Sendbuffer); lpreplybuffer = malloc (ireplysize ); /* Send ICMP messages and wait for the send-back Request */ Dwretval = icmpsendecho (hicmp, uldestipaddr, sendbuffer, Sizeof (Sendbuffer), null, lpreplybuffer, ireplysize, dwtimeout ); If (0! = Dwretval ){ // Obtain the icmp_echo_reply structure instance of the Request echo using a pointer. Picmp_echo_reply preplyecho = (picmp_echo_reply) lpreplybuffer; in_addr replyipaddr; replyipaddr. s_un.s_addr = preplyecho-> address; printf ( "Reply from: % s \ n" , Inet_ntoa (replyipaddr )); If (Dwretval> 1) {printf ( "Retrieved % d ICMP Response Message. \ n" , Dwretval); printf ( "---- The infomation for the first message. ---- \ n" );} Else {Printf ( "Retrieved % d ICMP reponse message. \ n" , Dwretval); printf ( "---- The information for the message. ---- \ n" );} Printf ( "The Status: % LD \ n" , Preplyecho-> status); printf ( "The reply data: % s \ n" , Preplyecho-> data); printf ("The roundtrip time: % LD milliseconds. \ n" , Preplyecho-> roundtriptime );} Else {Printf ( "ICMP send failure. \ n" ); Printf ( "The icmpsendecho call error: % LD \ n" , Getlasterror (); icmpclosehandle (hicmp ); Return Exit_failure ;} /* Close the ICMP handle */ Icmpclosehandle (hicmp ); Return Exit_success ;}

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.