Counting sucessful ping

Hello I need to count how much ping is sucessful, i use ICMPPing library. But I dont know how to take variable from library to my code. Here is part from library so i need count how much operator is true. Thats how much ping is sucessfull.

class ICMPPing
{
public:
    ICMPPing(SOCKET s); // construct an ICMPPing object for socket s
    bool operator()(int nRetries, byte * addr, char * result); // Ping addr, retrying nRetries times if no response is received. 
    // The respone is store in result.  The return value is true if a response is received, and false otherwise.

The operator() will return either true or false, if its true, just increment something.

Just use 'retries' set as zero then it will do a single ping, so if you want say four tries to work out an average, etc... then you can use a loop to call it multiple times. Simply use an 'if' statement to increment a variable only if the lib returns true.

Or see what the lib says about the result string it returns.

I know, I use ping but when I send ping 10 times but just 6 times Echo reply message come i need to write on display this prediction 6/10.

I dont know how to declarate this operator in code.

Have a read up on C or C++ loops, like 'for' and 'while'. You'll need an 'if' statement also.

Thats right, but i dont know how to take this variable

ICMPPing.operator() - write error; operator() - write error; ICMPPing.waitForEchoReply() - write error;

That operator allows the class instance to be used like a standard function. You'll also need an instance, it is a member function.

ICMPPing ping( socket ); //declare a variable & construct object.

//Use operator. ping( retries, addr, result );

I have it this way, it makes 5times ping, but I still dont know how much is sucessfull (that means no Request timed out message).

for(int i=1; i<6; i++){
  ICMPPing ping( socket ) ;
  ping( pingTimeout , destIp , strLineResult ) ;  
  myGLCD.print( strLineResult,0,d);
  d=d+10;
  char v[8];
  sprintf(v,"%d z 5",i);
  myGLCD.print(v,110,200);
  delay(500) ;

The operator call returns true or false so check that!

bool  result = ping( pingTimeout , destIp , strLineResult );

if( result ){

  //ping successful.

}else{

  //ping failed.

}

//Also, you can put ICMPPing ping( socket ) ; above the loop, as you can use the same object to ping many times.

Thank you, now it is right. And some kind of idea how to take a time average ping time ?

What is contained in strLineResult.

If the time is in there, extract it and add all the times together then divide by number of pings. If there is no time data, you can use millis to measure the call time.

long start = millis();
ping(...);
long duration = millis() - start;

Can you post a link to the library you are using. I searched and found many different versions with the same name.

pYro_65: Can you post a link to the library you are using. I searched and found many different versions with the same name.

I was wondering the same thing myself. In the version that I was looking at, the overloaded () operator call didn't return a value at all, and the parameters were in a different order:-

void operator()(const IPAddress& addr, int nRetries, ICMPEchoReply& result);

The ICMP library I'm using returns a char array in the third parameter... Sorry, I can't cut n paste right now, it here's what I've been doing reliably...

Instantiate your ICMP along with a replyBuffer[], then do something like this...

/ **** incoming reply is pointed to by ptr...

if (lPtr = strstr(ptr, "+CCLK:")) { // time message 

      // harvest the time from the modem reply string
      // to be more robust, we should parse the colons and commas...
      char temp[4];
      theTime.hour = (char)atoi(strncpy(temp, lPtr+17, 2));
      theTime.minute = (char)atoi(strncpy(temp, lPtr+20, 2));

      if (theTime.minute > 0) { // time is anything other than :00
    timeIsValid = true; // as soon as time is not midnight for the first time - it is 'valid'

char buf[6];
sprintf(buf, "%02d:%02d", theTime.hour, theTime.minute);
            }

here is that library in attachment, and here is a part from library i need take variable time

bool ICMPPing::operator()(int nRetries, byte * addr, char * result)
{
	W5100.execCmdSn(socket, Sock_CLOSE);
	W5100.writeSnIR(socket, 0xFF);
	W5100.writeSnMR(socket, SnMR::IPRAW);
	W5100.writeSnPROTO(socket, IPPROTO::ICMP);
	W5100.writeSnPORT(socket, 0);
    W5100.execCmdSn(socket, Sock_OPEN);
	bool rval = false;
	for (int i=0; i<nRetries; ++i)
	{
		if (sendEchoRequest(addr) < 0)
		{
			sprintf(result, "sendEchoRequest failed.");
			break;
		}
		if (waitForEchoReply())
		{
			uint8_t TTL;
			byte replyAddr [4];
			time_t timeSent;
			receiveEchoReply(replyAddr, TTL, timeSent);
			time_t time = millis() - timeSent;
			sprintf(result, "from:%d.%d.%d.%d bytes=%d time=%ldms TTL=%d", replyAddr[0], replyAddr[1], replyAddr[2], replyAddr[3], REQ_DATASIZE, time, TTL);
			rval = true;
			break;
		}

ICMPPing.h (2.04 KB)

ICMPPing.cpp (3.79 KB)