Search code examples
c++linux

How to ping in C++ under Linux


I have to call ping from C++ code. I'd like to easily read the output for further utilization.

I have come up with two solutions:

  • use a fork and a pipe, redirect ping output to the pipe and then parse it
  • find a library suited for the purpose to use a ping(ip_addresss) function directly

I'd like the latter, but I didn't find anything that was clearly a standard solution.

How would you do it?


Solution

  • I would go with your first option. Linux is built around the concept of having small, specialized apps which do one thing really well, communicating with pipes. Your app shouldn't include a library to implement ping, since there is already a built-in command to do it, and it works very well!