Is there any way to ping a specific IP address with C?

后端 未结 4 1801
情歌与酒
情歌与酒 2020-12-06 05:35

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 ne

4条回答
  •  Happy的楠姐
    2020-12-06 06:34

    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.

提交回复
热议问题