hello.. I am trying to write a code which can ping multiple ip addresses and depending on the reply print whether the server is on. My approach is to save the addresses in an array, then ping them using a for loop.
I am not sure about the data type i must use for ip addresses and how to use the system ping command after the array is created.
pls help..

2 Years
Discussion Span
Last Post by Aish_14

Here is my code. In this code i am simply printing the ip addresses, and it works fine. But actually, i need to ping them.

int main()
    char *str[100]; 
    int i,n;
    int num=15;
    printf("Enter the number of servers:\n");
    printf("Enter the ip address:\n");
       str[i]=(char *)malloc((num+1)*sizeof(char));
      printf("\n %s", str[i]);

Well, ping is a UDP protocol. There is a message format. You need to format a ping message, open a reply port, and send the message (with reply port) to the IP address using UDP, and then wait for the reply (usually using the select() function). You will need a timeout with select so you can continue sending pings until you reach some failure (or success) threshold and quit sending to that IP.

So, you have a lot more work to do in order to get this functioning. One alternative (faster to implement) would be to utilize the system() or exec...() commands to execute the system's ping executable directly for each IP address. You will still have to parse the response data to determine if ping succeeds or not.


Thank you for your help rubberman. I am using the system() command to ping and to check if th server replies or not I simply use an if() condition,
eg: if(system ("ping")==0), and it works perfectly fine. All I need to know now is how to ping an ip address which is stored in an array.


same as any other, except you get it out of the array first.
If you don't know how to, learn about arrays...


Thank you jwenting. But I didnt understand what do you mean by getting the ip address out of the array. I mean can I not use it directly?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.