我希望编写一个C或C++程序,可以输入一个IP地址进行ping操作并根据Ping的成功与否执行进一步的操作。如何实现呢?
我希望编写一个C或C++程序,可以输入一个IP地址进行ping操作并根据Ping的成功与否执行进一步的操作。如何实现呢?
编辑:我发帖后发现你在用Ubuntu。然而,对于搜索此问题的人来说,以下链接仍可能有用。
Ping:原始套接字方法:http://tangentsoft.net/wskfaq/examples/rawping.html
使用Icmp.dll实现Internet Ping:http://support.microsoft.com/default.aspx?scid=kb;en-us;170591
IcmpSendEcho函数:http://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx
Windows下的Ping:http://www.codeproject.com/KB/IP/winping.aspx
#include <iostream>
using namespace std;
int main() {
int x = system("ping -c1 -s1 8.8.8.8 > /dev/null 2>&1");
if (x==0){
cout<<"success";
}else{
cout<<"failed";
}
将你的IP地址中的8.8.8.8替换掉