Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting a return value from ping

Posted on 2003-02-27
5
Medium Priority
?
444 Views
Last Modified: 2008-02-26
Hi,

Does anyone know if there's a way of calling a ping and analyzing the results from within your program, other than using "system(ping)", piping the results somewhere, and then parsing the output ?
Basically i need to be able to ping an ip address every n seconds, and then if it is unsucessful, do various things. But i can see no way of calling a ping from within a C program. I'm using window NT so cannot use the ICMP stuff  (ie sendecho etc).
So it's like:

//Do a ping somehow...
if (ping was succesful)
//do stuff
else
//do some other stuff

but how can you get that ping result ?

Thanks a lot...
0
Comment
Question by:NT1
5 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8033139
>> but how can you get that ping result ?

Here is a simple way of doing this, although I would not call it elegant.

int r = system( "ping 66.218.71.198" ); // Ping yahoo.com

cout << "Process returned: " << r << endl;

This will return 0 if the ping was successful and 1 if the it failed.

Exceter
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8033220
An easy way to do this without storing the results would be to use a pipe, like:

#include <stdio.h>

int main()
{
  char line[1024] = {0};
  FILE *pipe = _popen("ping www.experts-exchange.com", "r");
  if(pipe == NULL)
    return -1;
  while(fgets(line, 1024, pipe))
  {
    // Parse it
  }
  _pclose(pipe);

  return 0;
}
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 8033676

This solution is not elegant either, but...

you can make a system call like this:

char* szPing = "ping yahoo.com > c:\\ping.txt";
system( szPing );

and then parse through the file you created (c:\ping.txt) and examine its contents.

0
 
LVL 2

Accepted Solution

by:
bkrahmer earned 150 total points
ID: 8039582
NT1, you can ping IP addresses directly, and you can do ICMP.  You need to use raw sockets, or the ICMP library.  There is some great info at http://www.sockets.com/ms_icmp.htm

brian
0
 

Author Comment

by:NT1
ID: 8040825
Thanks guys, i'm getting there...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question