?
Solved

Getting a return value from ping

Posted on 2003-02-27
5
Medium Priority
?
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

777 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