Qt 查看IP是否在线的两种方法

来源:互联网 发布:巴特尔在nba数据 编辑:程序博客网 时间:2024/06/07 23:25


1、使用QProcess和Ping


            QProcess *cmd = new QProcess;              #ifdef _TTY_ARMV4_              QString strArg = "ping -s 1 -c 1 " + b_tmpIpStr;        //linux平台下的格式              #else              QString strArg = "ping " + b_tmpIpStr + " -n 1 -w " + QString::number(m_timeoutInt) ;  //windows下的格式              #endif              cmd->start(strArg);              cmd->waitForReadyRead();              cmd->waitForFinished();              QString retStr = cmd->readAll();              if (retStr.indexOf("TTL") != -1)              {                  qDebug() << m_curIpStr <<"is online!\n";              }              retStr.clear();


2、使用QTcpSocket连接


            QTcpSocket socket(0);            socket.abort(); //取消原有连接            socket.connectToHost(m_curIpStr,m_curPort); //建立一个TCP连接            if(socket.waitForConnected(m_timeoutInt))            {                qDebug()<<m_curIpStr<<"is online\n";            }            socket.close();


1 0