有什么办法来ping用C特定的IP地址? [英] Is there any way to ping a specific IP address with C?

查看:107
本文介绍了有什么办法来ping用C特定的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来ping用C特定的IP地址?
如果我想平www.google.com一定数量的ping,或者就此而言,本地地址,我需要一个程序来做到这一点。我怎样才能从C ping通?

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?

推荐答案

您使用可以精心自己的 ICMP数据包< A HREF =htt​​p://mixter.void.ru/rawip.html>原始套接字,而是从小事远远。该源$ C ​​$为Ç平(1) 是一个很好的开始的地方就搞清楚如何做到这一点(它使用一个类似BSD许可证;看到源$ C ​​$ C为完整的许可证)。请记住,原始套接字要求对Linux的root权限,所以你的程序将需要setuid root的。

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.

当然,它更容易掏出到平(1)的可执行文件,而不必处理任何的这个自己。你将不必担心code牌,和你的程序将不再需要root权限(假设它还不需要他们别的东西)。 系统(3) ,的 的popen(3) 和的 叉(3) /的 执行exec(3) 是你的朋友。

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.

这篇关于有什么办法来ping用C特定的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆