bool GlobalStuff::PING(const QString &host_address, bool show_results){ if(host_address.isEmpty())return false;#ifdef DEBUG qDebug()<<"core: trying to ping host:"<<host_address;#endif bool boolRESULT=false; int ping_timeout=500; QString exe_path; #ifdef Q_WS_WIN exe_path=getwindir()+"\\system32\\ping.exe"; #endif #ifdef Q_WS_X11 exe_path="/bin/ping"; #endif exe_path=global_settings->value("main/ping",exe_path).toString(); ping_timeout=global_settings->value("main/SETTINGS_PING_TIMEOUT",ping_timeout).toInt(); if (ping_timeout<=0)ping_timeout=500; if(!QFile::exists(exe_path)) {#ifdef DEBUG qDebug()<<"core: ERROR:"<<exe_path<<"not found!";#endif boolRESULT=false; } else { //qApp->setOverrideCursor(Qt::WaitCursor); QStringList arguments; #ifdef Q_WS_WIN QTextCodec::setCodecForCStrings(QTextCodec::codecForName("cp-866")); arguments<<host_address; #endif #ifdef Q_WS_X11 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); arguments<<host_address<<"-c 1"; #endif QProcess myProcess; myProcess.start(exe_path,arguments); myProcess.waitForFinished(ping_timeout); QStringList strlstPingResults; strlstPingResults<<myProcess.readAll(); myProcess.deleteLater(); QString strResult=""; for (int i=0;i<=strlstPingResults.count()-1;i++) {#ifdef DEBUG qDebug()<<"core: ping results:"<<strlstPingResults[i];#endif strResult='\n'+strlstPingResults[i]; boolRESULT=strlstPingResults[i].contains("ttl",Qt::CaseInsensitive); if (boolRESULT)break; }; //qApp->restoreOverrideCursor(); if ((show_results)&&(boolRESULT))QMessageBox::information(0,host_address,strResult,QMessageBox::Ok); } return boolRESULT;}
class ConnectToRemoteHost : public QObject{ Q_OBJECTpublic: explicit ConnectToRemoteHost(QObject *parent = 0, const QString & = "0.0.0.0", const int & = 100); ~ConnectToRemoteHost();signals:public slots: void getHostStatus();private: QString hostname; QHostInfo info; QStringList portList; QTcpSocket *socket; QTimer * timer; bool onlineOfline;};ConnectToRemoteHost::ConnectToRemoteHost(QObject *parent, const QString & rhostname, const int & rtimeOut) : QObject(parent){ info = QHostInfo::fromName(rhostname); if (info.addresses().isEmpty()) { hostname = "0.0.0.0"; } else { hostname = info.addresses().first().toString(); } socket = new QTcpSocket(this); timer = new QTimer(this); onlineOfline = false; portList << "25" << "46" << "50" << "51" << "80" << "88" << "110" << "119" << "135" << "137" << "139" << "143" << "389" << "445" << "500" << "563" << "636" << "993" << "995"; connect(timer, SIGNAL(timeout()), this, SLOT(getHostStatus())); timer->start(rtimeOut);}void ConnectToRemoteHost::getHostStatus(){ onlineOfline = false; for(int i=0; i<portList.length(); i++) { socket->connectToHost(hostname, portList.at(i).toInt()); if(socket->waitForConnected(100)) { onlineOfline = true; socket->disconnectFromHost(); break; } } qDebug() << onlineOfline;}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); ConnectToRemoteHost * host = new ConnectToRemoteHost(0, "www.google.ru", 100); QThread *workerThread = new QThread; QObject::connect(workerThread, &QThread::started, host, &ConnectToRemoteHost::getHostStatus); QObject::connect(workerThread, &QThread::finished, host, &ConnectToRemoteHost::deleteLater); host->moveToThread(workerThread); workerThread->start(); return a.exec();}