[Solved] Qt application sometimes is unable to connect to Internet?
-
Hi,
In my application I have provided functionality to check for updates. This is done by reading an html file hosted on Google servers. This code works on some hardware while on some it just does not work. Probably because of Firewall or bad Anti-virus. But I am not sure what is the reason. So I wanted to know whether Qt provides any functionality to check why connection to the Internet is not happening? Probably an error message would do. Here is my code. I found it on one of the Wiki's but cannot remember the link.@//Header File
#ifndef CHECKUPDATES_H
#define CHECKUPDATES_H#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>class CheckUpdates : public QObject
{
Q_OBJECT
public:
explicit CheckUpdates(bool *finish, QString *link);void checkUpdates();
signals:
public slots:
private slots:
void fileDownloaded(QNetworkReply *reply);
void killChecking();private:
QNetworkAccessManager manager;
QTimer timer;
bool *finished;
QString *url;
};#endif // CHECKUPDATES_H
//Source File
#include "checkupdates.h"
#include <QDebug>CheckUpdates::CheckUpdates(bool *finish, QString *link) : QObject()
{
finished = finish;
url = link;
}void CheckUpdates::fileDownloaded(QNetworkReply *reply)
{
QString line = reply->readLine().simplified();
if(line.contains("Secret Keyword"))
{
//success!
}
else
{
qWarning("Failure checking Updates");
qDebug() << line << endl;
qDebug() << reply->readAll().simplified();
*url = "fail";
}reply->deleteLater(); *finished = true;
}
void CheckUpdates::killChecking()
{
disconnect(&manager, 0, 0, 0);
disconnect(&timer, 0, 0, 0);
*finished = true;
}void CheckUpdates::checkUpdates()
{
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileDownloaded(QNetworkReply*)));
connect(&timer, SIGNAL(timeout()), this, SLOT(killChecking()));
QNetworkRequest request(QUrl("https://googledrive.com/host/funky_code_by_Google/myFile.html"));
manager.get(request);
timer.start(10000);
}@On some PC's this is working just fine, while on some the request gets killed by the timer. The strange thing is that the qWarning() is not getting thrown! Why is this strange stuff happening?
-
To check an error message use reply->error().
qWarning is not called because fileDownloaded never gets called most likely.
-
Thanks for your reply. I will check error() and it in my code.
But can you please tell me why fileDownloaded() does not get called?
It cannot be that connection is not happening because it would throw a qWarning() internally. But since there is no such warning the connection must be happening. -
When the timer runs out you're disconnecting from QNetworkAccessManager::finished() signal which would call fileDownloaded should the connection actually finish.
-
so should I refrain from using such a timer?
But if I remove the timer, is it guaranteed that fileDownloaded() would be called eventually.
Also if this is called after a fixed time interval then is it possible to set this time interval?
So that I would no longer be needing the timer then. -
I don't think it's possible to set the timeout in the manager I think you'd be waitnig forever for the connection to timeout.