Alexa
Alexa

Reputation: 11

How do I check if the server I'm trying to access is enabled?

I'm writing a small application where I want to connect to a server.: we exchange data with him. Everything works fine when the server is connected, but I do not know how to check if it is turned on at all. I need this to display a message to the user that there is currently no physical connection to the server. I had the idea of checking for errors (because if the server is physically turned off, then there should be a timeout error - that's if it's logical), but by connecting the check in different places, it's like I don't check at all if there's any error. Tell me how I can check if the server is turned on at all (or fix my code). I'm sorry if I wrote something incomprehensible, I'm just starting to study the topic.

My parts of the code:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow){
ui->setupUi(this);

managerRead = new QNetworkAccessManager(this);
checkState = new QNetworkAccessManager(this);

tmr = new QTimer(this);
tmr->setInterval(10000);

//QNetworkRequest tap(QUrl(url));
//QNetworkReply *rep = checkState->get(tap);



connect(managerRead, &QNetworkAccessManager::finished, this, &MainWindow::replyFinishedRead);
connect(checkState, &QNetworkAccessManager::finished, this, &MainWindow::replyFinishedState);
connect(tmr, &QTimer::timeout, this, &MainWindow::timerStop);

/*connect(rep, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
    [=](QNetworkReply::NetworkError code){ qDebug()<<"error "<<rep->errorString(); qDebug()<< code;});*/

tmr->start();
checkState->get(QNetworkRequest(QUrl(url)));}

...

void MainWindow::replyFinishedState(QNetworkReply* repState){
QString result = repState->readAll();
QString som = "\"I'm alive.\"";checkReply = true;

if (checkReply){
    ...

    QNetworkRequest tap(QUrl(url));
    QNetworkReply *rep = checkState->get(tap);
    connect(rep, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
        [=](QNetworkReply::NetworkError code){ qDebug()<<"error "<<rep->errorString(); qDebug()<< code;});

    repState->deleteLater();
}

}

Upvotes: 1

Views: 48

Answers (0)

Related Questions