C ++ Ping source code (Linux/Unix)

Source: Internet
Author: User
Preface

In the project, you need to determine whether the host can be pinged. Therefore, you can find the ping source code using C language on the Internet, but it is difficult to reuse it and improve it: Add timeout processing and fault tolerance Processing, with the encapsulation of C ++ classes, the call is simpler.

BenCodeTested on both Ubuntu and FreeBSD, and theoretically compatible with Linux and UNIX

Source codeIntroduction

The source code is divided into three files: Ping. h Ping. CPP main. CPP, where ping. H and Ping. CPP provides ping operations, Main. CPP is an example of using the ping class. You have used the ping class to write a simple PingProgram. The Ping output is moved to main. cpp, so the output is concentrated in Main. cpp. The main Ping logic is processed in Ping. h ping. cpp.

In actual use, you only need to ping. h ping. cpp.

Sample Code

 

The sample code is very simple. As long as the ping class is instantiated and the ping operation is called, the result is returned through pingresult. The ping method returns false, which may be because the host and IP address are invalid, cannot be converted to a valid IP address.

 

If you want to determine whether a host can be pinged, you can determine whether the isreply of icmpechoreply returned by pingresult is true, true indicates that the Ping is responded, and false indicates that the Ping is not responded.

 //  Main. cpp  
# Include " Ping. h "
# Include <stdio. h>

Void Showpingresult (pingresult );
Int Main ( Int Argc, Char * Argv [])
{
If (Argc! = 2 ){
Printf ( " Usage: Ping " );
Return 0 ;
}
Char * Hostorip = argv [ 1 ];
Int Nsend = 0 , Nreceived = 0 ;
Bool RET;
Pingresult;
Ping = Ping ();
For ( Int Count = 1 ; Count <= 4 ; Count ++ ){
Ret = ping. Ping (hostorip, 1 , Pingresult );
If (COUNT = 1 ){
Printf ( " Ping % s (% s): % d bytes data in ICMP packets. \ n " , Hostorip, pingresult. IP. c_str (), pingresult. datalen );
}
If (! RET ){
Printf ( " % S \ n " , Pingresult. Error. c_str ());
Break ;
}
Showpingresult (pingresult );
Nsend + = pingresult. nsend;
Nreceived + = pingresult. nreceived;
}
If (RET ){
Printf ( " % D packets transmitted, % d already ed, % d lost \ n " , Nsend, nreceived,
(Nsend-nreceived)/nsend * 100 );
}
Return 0 ;
}

Void Showpingresult (pingresult ){
For (UnsignedInt Icmpechoreplyindex = 0 ; Icmpechoreplyindex <pingresult. icmpechoreplys. Size (); icmpechoreplyindex ++ ){
Icmpechoreply = pingresult. icmpechoreplys. At (icmpechoreplyindex );
If (Icmpechoreply. isreply ){
Printf ( " % D byte from % s: icmp_seq = % u TTL = % d RTT = %. 3f Ms \ n " ,
Icmpechoreply. icmplen,
Icmpechoreply. fromaddr. c_str (),
Icmpechoreply. icmpseq,
Icmpechoreply. ipttl,
Icmpechoreply. RTT );
} Else {
Printf ( " Request timeout \ n " );
}
}
}

 

Download and use code

Download Code

Compile

 
G ++ ping. cpp main. cpp Ping

Sample programs

 #  Ping hostname   
. /ping www.baidu.com
# test IP Ping
. /ping 8.8.8.8
# test the ping of an IP address that does not exist
. /ping 8.8.8.1
# test the IP address or host Ping
. /ping @ # %$ @#%#@$ %
Related Article

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.