C++ Boost.asio Ping

2019-04-13 02:06发布

问题:

I'm trying to make a program that will list all of the IP addresses of devices on a network. One of the main components of this is being able to ping devices. This program must work on Linux, Windows and Mac, so I chose the Boost library.

I managed to find this example in the documentation: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/icmp/ping.cpp

I am fairly experienced at C++ when using OpenGL 3.1 and Shaders but when I run this program it pings over and over (454+) and understanding it is throwing me for a loop (No pun intended) , it seems to contain a substantial amount of shorthand, functions I've never seen etc.

Is there a simpler method to ping a machine with Boost:asio? or Is there a way I can get this method to run just 4 times and return the average response time?

回答1:

In the linked example you have to change the handle_receive function to include a counter. When it has been called four times you tell the io_server object to stop (for this you either has to make it global, or store a reference to it in the pinger class) and not call start_receive.