tags:

views:

418

answers:

4

Hi!

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 :)

A: 

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.

Jacob
I use this "solution" right now, but it would be easier, to get the return value, and then i shouldn't work with files. That's why, i want to know how to capture the return value :)
kampi
+1  A: 
Heath Hunnicutt
+2  A: 

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

cmeerw
A: 

So you just want to know the return value: i.e. "0 on success, 1 on failure such as unknown host, illegal packet size, etc. and 2 On a unreachable host or network." I think using ping is an overkill in this case. Writing to file and reading from it is apparently a little bit ugly.

I think you should just try open()ing a connection to the host on port 7 (Echo). Then you would get the information that ping return value would tell you. If you further want to get response durations, you can measure it yourself by sending arbitrary data to the host on echo port. It would take some time to write the code but i think it's worth for flexibility and accuracy.

ercan