int sockfd;
struct sockaddr_in dstaddr;
bool ret;
char* ipAddr;
ipAddr = GetIPAddress(m_wirelessNetworkDevice);
if(!IsValidIPAddress(ipAddr.toUtf8().constData()))
return false;
if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) //ICMP协议,原始套接字类型
{
err_sys("socket");
return false;
}
bzero(&dstaddr, sizeof(dstaddr));
dstaddr.sin_family = AF_INET;
dstaddr.sin_port = htons(0);
//printf("dest is %s\n",dest);
if (inet_pton(AF_INET, dest, &dstaddr.sin_addr) <= 0)
{
err_sys("inet_pton");
::close(sockfd);
return false;
}
ret = send_echo_req(sockfd, &dstaddr);
if (!ret)
{
::close(sockfd);
return false;
}
ret = recv_echo_reply(sockfd);
if (ret)
{
::close(sockfd);
return true;
}
else
{
::close(sockfd);
return false;
}
}
以上是Linux下的代码。