tags:

views:

389

answers:

3

Using C++, I would like to use a command/class to get the latency time from pinging a host to use in my program. I tried using the ping command but there was no easy way to gather the time since its included with other statistical information. I was hoping for an easier approach.

+5  A: 

The ping tool is usually implemented in C and works by sending ICMP Echo request packets over a raw socket,. The system time is recorded -- usually with gettimeofday on under posix -- when the Echo request is made and again when an Echo reply (if any) is received to determine the round-trip time. You can put the same functionality in your C++ application using sockets.

Otherwise, extracting the information from a ping system call is probably easier than you think. The key is to open a pipe to allow reading of the standard output of the ping command (see popen or _popen). A regular expression (e.g. "time=([0-9]*)") could be used to pluck out the desired data. If you don't have a regex library available, then extracting this data only requires fairly trivial string manipulation. The STL string class provides several algorithms that may be of use.

Judge Maygarden
Raw sockets require "root" or "Administrator" privileges. On UNIX the ping program is suid so you can run it as a normal user, but you can't write your own version unless your program runs as root. This may or may not be a problem for you. The RE approach is probably better.
user9876
+1 for the hyperlinks!
Johann Gerell
Thanks. I'm able to figure out how to parse the information that I need when I run the command from the command prompt. However, in my program, the return value for a ping command is either 0 or 1. Is there an easier way to get my info rather than redirecting the output to a flatfile?
Victor
Yes, use popen to read stdout from ping.
Judge Maygarden
A: 

if you don't like regexes you can always loop through the output, looking for t followed by i followed by m followed by e followed by = by just stepping through the output string one piece at a time. store the pointer to the char after =, then step through futher and replace the next space by a zero. you now have a string with the latency, converting to a number can be done with the existing conversion functions.

e.g. if the output is in char* output with length stored in unsigned int length and the matched string needs to go in char* match...

for(unsigned int i = 4; i < length; ++i)
{
  if(output[i] == '=')
  {
    if((output[i-4] = 't') && (output[i-3] = 'i') && (output[i-2] = 'm') && (output[i-1] = 'e'))
    {
      match = &(output[i+1]);
      while(output[i] != ' ') ++i;
      output[i] = 0;
      break;
    }
  }
}

regexes are nicer though... they make your code look a lot a tider and readable. although if you don't have a library for them it will be faster to just implement it like this... :)

jheriko
A: 

I understand this but the return value from a ping command is either 0 or 1. Is there an easier way than rediecting the standard output to a flatfile?

Victor
This not a forum, keep your comments in the comment section of the reply you're commenting on.
Milan Babuškov