start(orderInfo)_qt ping">
赞
踩
方法(1) QProcess调用命令行,读取标准输出即可。
QProcess pingProcess; QString strArg = "ping " + strPingIP + " -n 1 -i 2"; //strPingIP 为设备IP地址 pingProcess.start(strArg,QIODevice::ReadOnly); pingProcess.waitForFinished(-1); QString p_stdout = QString::fromLocal8Bit( pingProcess.readAllStandardOutput()); qDebug()<<p_stdout ; bool bPingSuccess = false; if(p_stdout.contains("TTL=")) //我采用这个字符来判断 对不对? { bPingSuccess = true; }else { bPingSuccess = false; }
方法二(2) 不建议使用QProcess调用命令行然后解析返回字符串来判断。我曾经用过这种方法,会程序崩溃。我的代码是完全没问题。后来我是重写了ping来实现的。就是自己从更底层去实现这个ping,其实代码量不大的。源码也是有标准的。你拿下来改改就OK
```ctypedef struct tagIPHDR { u_char VIHL; // Version and IHL u_char TOS; // Type Of Service short TotLen; // Total Length short ID; // Identification short FlagOff; // Flags and Fragment Offset u_char TTL; // Time To Live u_char Protocol; // Protocol u_short Checksum; // Checksum struct in_addr iaSrc; // Internet Address - Source struct in_addr iaDst; // Internet Address - Destination }IPHDR, *PIPHDR; // ICMP Header - RFC 792 typedef struct tagICMPHDR { u_char Type; // Type u_char Code; // Code u_short Checksum; // Checksum u_short ID; // Identification u_short Seq; // Sequence char Data; // Data }ICMPHDR, *PICMPHDR; // ICMP Echo Request typedef struct tagECHOREQUEST { ICMPHDR icmpHdr; struct timeval echoTime; char cData[32]; }ECHOREQUEST, *PECHOREQUEST; // ICMP Echo Reply typedef struct tagECHOREPLY { IPHDR ipHdr; ECHOREQUEST echoRequest; char cFiller[256]; }ECHOREPLY, *PECHOREPLY; bool Utilities::ping(const char *hostName) { if (NULL == hostName) { XgsLogger::log("", QtWarningMsg, "(%s,%d), Invalid IP address!", __FILE__, __LINE__ ); return false; } int sockfd = 0; int nRet; int nCount; int iSuccess = 0; struct sockaddr_in addrDest; struct hostent *Dest; float spenttime; if ((Dest=gethostbyname(hostName)) == NULL) { return false; } if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { return false; } memset((char *)&addrDest, 0, sizeof(struct sockaddr_in)); addrDest.sin_addr = *((struct in_addr *)Dest->h_addr); addrDest.sin_family = AF_INET; bzero(&(addrDest.sin_zero), sizeof( addrDest.sin_zero ) ); ECHOREQUEST echoReq; memset((ECHOREQUEST*)&echoReq, 0, sizeof(struct tagECHOREQUEST)); echoReq.icmpHdr.Type = ICMP_ECHO; echoReq.icmpHdr.Code = 0; echoReq.icmpHdr.ID = getpid(); int Seq = 0; for (nRet = 0; nRet < 32; nRet++) echoReq.cData[nRet] = ' '+nRet; nCount = 0; while(nCount < 3) { echoReq.icmpHdr.Seq = Seq++; echoReq.icmpHdr.Checksum = 0; gettimeofday(&echoReq.echoTime,NULL); echoReq.icmpHdr.Checksum = checksum((unsigned short*)&echoReq, sizeof(struct tagECHOREQUEST)); if (sendto(sockfd, (ECHOREQUEST*)&echoReq, sizeof(tagECHOREQUEST), 0, (struct sockaddr *)&addrDest, sizeof(addrDest)) < 0) { close(sockfd); return false; } if(WaitForEchoReply(sockfd) == -1) { close(sockfd); return false; } ECHOREPLY icmpRecv; int addr_len; addr_len = sizeof(struct sockaddr); if (recvfrom(sockfd, (ECHOREPLY*)&icmpRecv, sizeof(struct tagECHOREPLY), 0, (struct sockaddr *)&addrDest, (socklen_t *)&addr_len) < 0) { close(sockfd); return false; } else if(icmpRecv.echoRequest.icmpHdr.Type == ICMP_ECHOREPLY) { gettimeofday(&icmpRecv.echoRequest.echoTime, NULL); tv_sub(&icmpRecv.echoRequest.echoTime, &echoReq.echoTime); spenttime=icmpRecv.echoRequest.echoTime.tv_sec*1000+icmpRecv.echoRequest.echoTime.tv_usec*0.001; if (strcmp(Dest->h_name, inet_ntoa(icmpRecv.ipHdr.iaSrc))==0) { iSuccess ++; // printf("Reply from %s: Bytes=%d Id_seq = %d time=%4.3fms TTL=%d\n",/*Dest->h_name*/inet_ntoa(icmpRecv.ipHdr.iaSrc), sizeof(icmpRecv.echoRequest), icmpRecv.echoRequest.icmpHdr.Seq, spenttime,icmpRecv.ipHdr.TTL); } else { ; //printf("From %s Id_seq %d Destination Host Unreachable ", inet_ntoa(icmpRecv.ipHdr.iaSrc), icmpRecv.echoRequest.icmpHdr.Seq); } // sleep(1); } nCount ++; } close(sockfd); if (iSuccess >0) return true; else return false; }
方法(3) 不实现ping功能,更为简单的方法 (经测试无效)
//判断IP地址及端口是否在线
static bool IPLive(QString ip, int port, int timeout = 1000) {
QTcpSocket tcpClient;
tcpClient.abort();
tcpClient.connectToHost(ip, port);
//100毫秒没有连接上则判断不在线
return tcpClient.waitForConnected(timeout);
}
//判断是否通外网
static bool IsWebOk() {
//能接通百度IP说明可以通外网
return IPLive("115.239.211.112", 80);
}
非原创,侵权必删!
参考文章:https://bbs.csdn.net/topics/391873625
https://www.cnblogs.com/judes/p/7069051.html
http://www.voidcn.com/article/p-cbvxdzwt-bny.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。