bool PING(QString strServer){ QStringList strlstPingResults; bool boolRESULT=false; QStringList arguments; #ifdef Q_WS_WIN QTextCodec::setCodecForCStrings(QTextCodec::codecForName("cp-866")); arguments<<strServer; #endif #ifdef Q_WS_X11 arguments<<strServer<<"-c 1"; #endif QProcess myProcess; myProcess.start("ping", arguments); myProcess.waitForFinished(500); strlstPingResults<<myProcess.readAll(); myProcess.deleteLater(); for (int i=0;i<=strlstPingResults.count()-1;i++){ qDebug()<<"DEBUG: core: ping results"<<strlstPingResults[i]; boolRESULT=strlstPingResults[i].contains("ttl",Qt::CaseInsensitive); if (boolRESULT)break; }; return boolRESULT;}
QSocket test; test.connectToHost(sql.value(1).toString(),sql.value(2).toInt()); int ii=0; while(test.state()!=QSocket::Connected) { ii++; if (ii==10) break; qApp->processEvents(); } if (test.state()==QSocket::Connected) { test.disconnect(); QMessageBox::critical( this,"","Есть связь"); } else { QMessageBox::critical( this,"","Нет связи"); }
bool MainWindow::CheckConnection() { bool boolConnected=false; QProgressDialog *conect2hostDialog = new QProgressDialog("Waiting for connection...", "Cancel", 0, 0); conect2hostDialog->setModal(true); conect2hostDialog->show(); //-------------------------------------------------------- QTcpSocket test; test.connectToHost(strHostName,strPort.toInt()); while(test.state()!=QTcpSocket::ConnectedState){ if (conect2hostDialog->wasCanceled()){boolConnected=false;break;}; qApp->processEvents(); }; if (test.state()==QTcpSocket::ConnectedState){ boolConnected=true; test.disconnect(); //QMessageBox::critical( this,"",tr("Connection established")); } else QMessageBox::critical( this,"",tr("Connection to ")+strHostName+":"+strPort+tr(" is failed.")); //-------------------------------------------------------- delete conect2hostDialog; return boolConnected;}
C++ (Qt)#include <QCoreApplication>#include <QTcpSocket>#include <QDebug> int main( int argc, char *argv[] ){ QCoreApplication app( argc, argv ); QTcpSocket socket; socket.connectToHost( "localhost", 3306 ); if( socket.waitForConnected() ) { qDebug() << "MySql server active"; } else { qDebug() << "MySql server not active"; } return 0;}
if (flagTestUsel) return; // Идет обработка flagTestUsel=true; // начинаем работу QSqlQuery sql(MyPSQL); // наша база sql.exec("SELECT * FROM usel"); int prz=0; while (sql.next()) // проверка всех серверов { QSocket test; test.connectToHost(sql.value(1).toString(),sql.value(2).toInt()); int ii=0; while(test.state()!=QSocket::Connected) { ii++; if (ii==10) break; qApp->processEvents(); } if (test.state()==QSocket::Connected) { prz=1; // есть связь test.disconnect(); } else { prz=0; // нет связи } if (prz!=sql.value(5).toString()) // статус изменился { QSqlCursor sCur("usel",true,MyPSQL); sCur.select("ind="+sql.value(7).toString()); if (sCur.next()) { QSqlRecord *buffer; buffer = sCur.primeUpdate(); // запись для обновления buffer->setValue("prz",prz); sCur.update(); } qApp->processEvents(); } } textLabel1->setText("Контроль серверов. Пауза в работе"); flagTestUsel=false; // закончили проверку
C++ (Qt)bool CheckConnection(){ bool boolConnected=false; QTcpSocket test; test.connectToHost(strCONNECTION_SETTINGS_Hostname,strCONNECTION_SETTINGS_Port.toInt()); qDebug()<<"DEBUG: core: polling server..."; if( test.waitForConnected() ){ boolConnected=true; qDebug()<<"DEBUG: core: connection established"; qDebug()<<"DEBUG: core: disconnecting..."; test.waitForDisconnected(); qDebug()<<"DEBUG: core: disconnected"; } else { boolConnected=false; qDebug()<<"DEBUG: core: connection failed"; }; return boolConnected;}
C++ (Qt) { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","mysql_db_core"); qDebug()<<"DEBUG: core: create connection"; db.setHostName(strCONNECTION_SETTINGS_Hostname); db.setPort(strCONNECTION_SETTINGS_Port.toInt()); db.setDatabaseName(strCONNECTION_SETTINGS_DBname); db.setUserName(strCONNECTION_SETTINGS_Username); db.setPassword(strCONNECTION_SETTINGS_Password); if (boolCONNECTION_SETTINGS_SSL) db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1"); // use an SSL connection to the server if (db.open()){ ... db.close(); }; }; qDebug()<<"DEBUG: core: remove connection.";QSqlDatabase::removeDatabase("mysql_db_core");