11 #include <boost/asio.hpp> 12 #include <boost/bind.hpp> 20 using boost::asio::ip::icmp;
21 using boost::asio::deadline_timer;
27 pinger(boost::asio::io_service& io_service,
const char* destination)
31 icmp::resolver::query query(icmp::v4(), destination,
"");
43 std::string body(
"\"Hello!\" from Asio ping.");
54 boost::asio::streambuf request_buffer;
55 std::ostream os(&request_buffer);
56 os << echo_request << body;
59 time_sent_ = posix_time::microsec_clock::universal_time();
71 std::cout <<
"Request timed out" << std::endl;
101 is >> ipv4_hdr >> icmp_hdr;
115 posix_time::ptime now = posix_time::microsec_clock::universal_time();
116 delay = (now -
time_sent_).total_nanoseconds()/(1000.0*1000);
119 <<
": icmp_seq=" << icmp_hdr.sequence_number()
122 <<
", time=" << delay <<
" ms" 131 #if defined(BOOST_WINDOWS) 132 return static_cast<unsigned short>(::GetCurrentProcessId());
134 return static_cast<unsigned short>(::getpid());
149 int main(
int argc,
char* argv[])
155 std::cerr <<
"Usage: ping <host>" << std::endl;
156 #if !defined(BOOST_WINDOWS) 157 std::cerr <<
"(You may need to run this program as root.)" << std::endl;
162 boost::asio::io_service io_service;
163 pinger p(io_service, argv[1]);
166 catch (std::exception& e)
168 std::cerr <<
"Exception: " << e.what() << std::endl;
void handle_receive(std::size_t length)
static unsigned short get_identifier()
boost::asio::streambuf reply_buffer_
posix_time::ptime time_sent_
unsigned short sequence_number_
icmp::endpoint destination_
pinger(boost::asio::io_service &io_service, const char *destination)
int main(int argc, char *argv[])