Alperen Görmez
Alperen Görmez

Reputation: 392

Pinging a server in C

I have a piece of code which pings a server:

int main(){
    if ( system( "ping 111.222.333.44") == 0 )
        printf("success");
    return 0;
}

The code prints the following:

PING 111.222.333.44 (111.222.333.44) 56(84) bytes of data.
64 bytes from 111.222.333.44: icmp_seq=1 ttl=63 time=0.565 ms
64 bytes from 111.222.333.44: icmp_seq=2 ttl=63 time=0.874 ms
.
.

and so on. Since success is never printed, I assume that system( "ping 111.222.333.44") somehow enters an infinite loop. When I hit Ctrl+C, the statistics are printed (min/avg/max/mdev etc.) and the program terminates. Is there a way to avoid this infinite loop?

Also, is the number of bytes sent to the server is 56 or 84? Or 64? And can we specify this value manually? If I were to calculate the speed during the process, would I do 56 bytes / 0.565ms = 99.1 KB/s? Thanks in advance.

Upvotes: 2

Views: 154

Answers (1)

glglgl
glglgl

Reputation: 91149

In short, ping can be configured in many different ways. With -c 4, you can specify that 4 ping packets are sent, with -s XX you can specify the packet size.

Upvotes: 1

Related Questions