文章作者:里海
来源网站:https://blog.csdn.net/WangPaiFeiXingYuan
简介:
ping是一种用于测试网络连接的工具,它通过发送数据包到目标设备并等待其响应来工作,以检查网络是否连通。下面是例子.
效果:
代码:
#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <cstring>
#include <string>
using namespace std;bool ping(const std::string& ipAddress)
{std::string command = string("ping -w 500") + " " + ipAddress;int status = system(command.c_str());return status == 0;
}int main()
{std::string ipAddress = "192.168.16.255";bool result = ping(ipAddress);if (result){std::cout << ipAddress << " can bet pinged" << std::endl;}else{std::cout << ipAddress << " cannot be pinged" << std::endl;}cin.get();return 0;
}