Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture ping's return value in C++

Tags:

c++

windows

Does anyone know, how to capture ping's return value in c++? According to this link: ping should return 0 on success, 1 on failure such as unknown host, illegal packet size, etc. and 2 On a unreachable host or network.

In C++ I called ping with the system (), e.g. int ret = system("ping 192.168.1.5");.

My problem is, that ret's value is 0 or 1. It will never 2! If I think correctly, this is because, this return value I get, is the system functions return value, not ping's. So how could i get ping's return vlaue?

Thanks in advance!

kampi

Edit: Right now i use this system("ping 192.169.1.5 > ping_res.txt"); but i don't want to work with files (open, and read them), that's why i want to capture, th return value , if possible :)

like image 706
kampi Avatar asked Dec 17 '25 00:12

kampi


2 Answers

If you are on Windows, it might be better to use IcmpSendEcho2 directly to implement the ping functionality in your application.

like image 175
cmeerw Avatar answered Dec 19 '25 14:12

cmeerw


A simple solution would be pipe the output of ping to to a file and read it.

E.g.

system("ping 192.169.1.5 > ping_res.txt");

And read ping_res.txt to get the info you need.

like image 35
Jacob Avatar answered Dec 19 '25 14:12

Jacob