0

I'm write a small console application (windows plateform using visual studio 2012) that show the current ping to an IP and some others stats. (average of the last 100pings, packet loss, etc...) Here is my actual implementation:

char buffer[100];
FILE *fp = _popen("ping -l 1 -n 1 61.5.222.121" , "r");
for (i=0;i<8;i++) {
   fgets(buffer,100,fp);
}
_pclose(fp)

I call this function in my main with:

_beginthread( ping, 0, NULL );

Then I extract the ping from buffer with strstr and sscanf_s. But when I execute the program, I see in the process manager that ping.exe and cmd.exe keeps opening/closing, which is normal because at each popen, it launch one.

I'm looking for a way to have a cleaner way of retrieving the ping time value. For example something that can read the output each time a new line is created in the popen (with the command "ping google.com -t"), this way there would be only one ping.exe and cmd.exe process that make all the job and the ping refresh time would be much more fast.

I've looked into the IcmpSendEcho command, but this looks very complicated for me. I basically want a simple function that runs into the background and write the ping response time in a global variable each time he recieve a new one.

cagou
  • 25
  • 6
  • You may want to read the second answer (the Windows one) of http://stackoverflow.com/questions/6697292/pinging-from-a-c-c-program – Matthieu Rouget Jun 25 '13 at 06:30

1 Answers1

2

If doing IcmpSendEcho seems complicated, perhaps this source code can help, which you modify for your needs.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85