Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Application hangs when using QNetworkAccessManager

Application hangs when using QNetworkAccessManager

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Ketan Shah
    wrote on last edited by
    #1

    Hi, I am trying to check for an internet connection using the following code.
    The problem I am facing is that whenever application is executed my antivirus prompts whether to allow the application to access the internet or not, if its allowed then its work properly, incase if there is no internet connection then the respective error message is shown, but in case if the application is not allowed to access the ineternet then my application hangs for around 3mins and after 3mins start responding again. what could be the possible solution for this, any kind of help will be appreciated.

    @mNetworkManager = new QNetworkAccessManager(this);
    url = "http://www.mysite.com/updates.php";
    reply = mNetworkManager->get(QNetworkRequest(url));
    QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));

    QString myapp::onNetworkReply(QNetworkReply* reply)
    {

    QString replyString;
    QFile file("temp.txt");
    //file.open(QIODevice::ReadWrite | QIODevice::Text);
    QDataStream f1(&file);

    if(reply->error() == QNetworkReply::NoError)
    {
    int httpstatuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();

    switch(httpstatuscode)
    {
        case RESPONSE_OK:
        if (reply->isReadable())
        {
            //Assuming this is a human readable file replyString now contains the file
    
            replyString = QString::fromUtf8(reply->readAll().data());
            ui->textEdit->append(replyString);
            //f1<<replyString;
        }
        else
        {
            if (file.open(QIODevice::WriteOnly))
            {
                f1 << reply->readAll();
                file.close();
            }
        }
        break;
        case RESPONSE_ERROR:
        QMessageBox::warning(this,"Error","There was an error in your internet connection",QMessageBox::Ok);
    
        case RESPONSE_BAD_REQUEST:
    
        case RESPONSE_BLOCKED:
        break;
    }
    

    }
    else if(reply->error() != QNetworkReply::NoError)
    {
    QMessageBox::warning(this,"Error","No internet connection",QMessageBox::Ok);
    }
    reply->deleteLater();

    return replyString;

    }

    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Have you tried halting your application using the debugger the find out where it actually stalls?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Ketan Shah
        wrote on last edited by
        #3

        No,
        but i guess its due to the reason that request to the url was not proceeded as the application was blocked from accessing the internet.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          Well, I do not consider "guessing" a valid debugging technique.

          What Qt version do you use? 4.8 got a threaded QNetworkAccessManager, so even though your request blocks your main thread shouldn't be affected.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Ketan Shah
            wrote on last edited by
            #5

            I am using Qt 4.7.0

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              QNetworkAccessManager should not block, irregardless of the threaded and non-threaded version, and this is most probably a side effect of the desktop firewall (which are known to do more harm than good). As far as I can tell right now you are basically left with three options:

              • upgrade to 4.8
              • use your own threaded mechanism
              • debug your application to find the actual cause so we can find a solution
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved