Is there any way to ping a specific IP address with C? If I wanted to ping "www.google.com" with a certain number of pings, or for that matter, a local address, I would need a program to do that. How can I ping from C?
-
Use the `socket` API (`man socket`). – Kerrek SB Nov 18 '11 at 22:29
-
Why would you want to ping Google, assuming you don't work for Google? – dbasnett Nov 22 '11 at 13:49
-
It is operating system specific. And `www.google.com` is not an IP address (but `192.168.2.34` is one). – Basile Starynkevitch Aug 30 '15 at 06:34
-
@dbasnett you really can't think of any possible reason? – Brennan Vincent Dec 02 '15 at 20:05
-
@BrennanVincent - unless I am a network person, no I can't. If you are a programmer and just want to know with some certainty that your network is functional then pinging your local gateway should be sufficient. – dbasnett Dec 03 '15 at 13:03
-
1How do you know if your local gateway is actually hooked up to the internet? – Brennan Vincent Dec 03 '15 at 19:57
4 Answers
There is no accepted answer yet and I stumbled upon this question while trying to do exactly what was asked here so I wanted to refer to Aif's answer here.
The following code is based on his example and pings Google's public DNS in a child process and prints the output in the parent process.
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#define BUFLEN 1024
int main(int argc, char **argv)
{
int pipe_arr[2];
char buf[BUFLEN];
//Create pipe - pipe_arr[0] is "reading end", pipe_arr[1] is "writing end"
pipe(pipe_arr);
if(fork() == 0) //child
{
dup2(pipe_arr[1], STDOUT_FILENO);
execl("/sbin/ping", "ping", "-c 1", "8.8.8.8", (char*)NULL);
}
else //parent
{
wait(NULL);
read(pipe_arr[0], buf, BUFLEN);
printf("%s\n", buf);
}
close(pipe_arr[0]);
close(pipe_arr[1]);
return 0;
}
-
The execl() command has an incorrect parameter. The parameter "-c 1" should be written as "-c", "1". – Joel Ravazzolo Mar 24 '22 at 15:24
You could craft your own ICMP packets using raw sockets, but that's far from trivial. The source code for ping(1)
is a good place to start on figuring out how to do that (it uses a BSD-like license; see the source code for the full license). Keep in mind that raw sockets require root privileges on Linux, so your program will need to be setuid root.
Of course, it's much easier to shell out to the ping(1)
executable and not have to deal with any of this yourself. You won't have to worry about code licensing, and your program won't need root privileges (assuming it doesn't already need them for something else). system(3)
, popen(3)
, and fork(3)
/exec(3)
are your friends.

- 390,455
- 97
- 512
- 589
-
I wish you have given a better explanation of the system/exec ping method. This would have been extremely helpful. – CaptainBli May 07 '13 at 17:21
-
You would have to learn sockets, resolve the host you want to ping, send the appropiate ICMP packages and listen for a response. There is no ping
function in the standard library. However, there are lots of higher level network libraries that already implement the protocol.

- 80,396
- 20
- 159
- 169
This is just the simplest I found.
http://www.cise.ufl.edu/~cop4600/cgi-bin/lxr/http/source.cgi/commands/simple/ping.c

- 2,723
- 2
- 18
- 26
-
1How would I locate the net/gen files? They seem to be in unavailable when I try and compile! – Matt Fletcher Sep 09 '13 at 07:49
-
1
-