You need to specify the time in the ping being sent, and take the data from the response to check if there is an ip on the network or not. Because low-level API functions (for example, built-in ICMP.DLL) seemed complicated. So far I have decided to stop at IdICMP. Tell me what libraries are needed for IdICMP and can anyone give a link to the article how to work with it (all references). I am writing MS VS C ++ 2008 Express Edition.
In google did not find, msdn for it is not, examples without #include.

#include "stdafx.h" #include "conio.h" #include <iostream>; #include "winsock2.h" #include "windows.h" #include "locale.h" int main() { IdIcmpClient1->ReceiveTimeout=1000; IdIcmpClient1->Host="77.88.21.3"; IdIcmpClient1->Ping(); _getch(); } 

Errors produced by the compiler:

 .\2.cpp(13) : error C2065: IdIcmpClient1: необъявленный идентификатор .\2.cpp(13) : error C2227: выражение слева от "->ReceiveTimeout" должно указывать на тип класса, структуры или объединения либо на универсальный тип тип: 'unknown-type' .\2.cpp(14) : error C2065: IdIcmpClient1: необъявленный идентификатор .\2.cpp(14) : error C2227: выражение слева от "->Host" должно указывать на тип класса, структуры или объединения либо на универсальный тип тип: 'unknown-type' .\2.cpp(15) : error C2065: IdIcmpClient1: необъявленный идентификатор .\2.cpp(15) : error C2227: выражение слева от "->Ping" должно указывать на тип класса, структуры или объединения либо на универсальный тип тип: 'unknown-type' 2 - ошибок 6, предупреждений 0 
  • Accidentally, in search of information on IdIcmp, I came across a ping source for Windows. Maybe it will be useful to you. - avp

1 answer 1

And you can do it in 10 minutes quite simply.

 #include <stdio.h> #include <stdlib.h> int main (int ac, char *av[]) { char buf[100]; sprintf (buf,"ping %s", av[1]? av[1] : "hashcode.ru"); FILE *f = popen(buf,"rb"); while (fgets(buf,100,f)) { printf ("buf = [%s]\n", buf); fflush(stdout); } pclose(f); return puts("End") == EOF; } 

It remains to see ping /? read the ping command flags and filter the lines readable in the loop (take what you need).

  • There are streams and it is not clear how they are connected to the network, how ping goes. I also did not understand these lines FILE * f = popen (buf, "rb"); pclose (f); The compiler issued that they are not declared, so here, at least there is not enough .h or .lib. It looks like writing to a file, but the question remains how it goes to the network and how ip is pinged. - ananas
  • @ananas, I have everything working in MinGW in Windows PS C: \ Users \ avp \ src \ cc \ hashcode> gcc. \ poping.c PS C: \ Users \ avp \ src \ cc \ hashcode>. \ a buf = [] buf = [Packet exchange with hashcode.ru [5.9.6.228] with 32 data bytes:] buf = [Answer from 5.9.6.228: number of bytes = 32 time = 59ms TTL = 52] buf = [Answer from 5.9.6.228 : number of bytes = 32 time = 58 ms TTL = 52 ... it just doesn’t fit ... buf = [Ping statistics for 5.9.6.228:] buf = [Packets: sent = 4, received = 4, lost = 0]. ....... End PS C: \ Users \ avp \ src \ cc \ hashcode> - avp
  • See also man popen . By the way, g ++ also works PS C: \ Users \ avp \ src \ cc \ hashcode> g ++. \ Poping.c PS C: \ Users \ avp \ src \ cc \ hashcode> echo $? True PS C: \ Users \ avp \ src \ cc \ hashcode> How does IP respond? Naturally the ping command (the one that you run on the command line). - avp
  • avp thanks for the clarification. I read man popen more clearly, I still don’t understand everything about threads, so I didn’t immediately understand the connection between ping and stream. I decided in another way: IcmpSendEcho [url = msdn.microsoft.com/en-us/library/windows/desktop/… The question is also on this topic, because ping goes on its ports, getting a positive ping is not sure whether any other port is open. Using what you can ping, for example, port 80, to find out if it is available. The method of sending http requests and accepting answers is decided, but maybe you can advise another way, like ping. - ananas
  • @ananas, in ICMP (it looks like you call it ping) there are no ports . Briefly about ICMP can be read, for example, on the wiki. In general, ping is a program that sends type 8 ICMP packets (Echo request) to a given IP address and reads ICAP reply packets of type 0 (Echo reply). - If you are interested in the 80th port and TCP protocol, then the easiest way is to create a normal TCP socket and just try to connect with it. - avp