Results 1 to 17 of 17

Thread: QHttp::get seems to hang on request

  1. #1
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QHttp::get seems to hang on request

    I have a class that uses QHttp and its get method to download multiple files. It does this fairly well although once and a while it will just hang after its done a few hundred files. I believe it hangs after sending the request. Any idea why this would be happening?

    Thank you in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    Hard to say without seeing any code. Are you sure this is caused by QHttp and not by your system, some firewall or the remote host? I've been using QHttp to download many files and I never experienced such behaviour.

  3. #3
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    I'll test it out with my firewall and such turned off and see if it changes anything.
    I know the host can handle it because I performed the same downloads with wget with no problem.

    If it still doesn't work, I'll post how my code looks.

  4. #4
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    I tried getting rid of the firewall and it didn't fix anything. Ok so basically I created an Http class with an interface like this:

    Qt Code:
    1. public:
    2. Http(int min, int max, QObject* parent=0);
    3. void downloadFiles();
    4. signals:
    5. void done();
    6.  
    7. private slots:
    8. void requestFinished(int id, bool error);
    9. void done(bool error);
    10. void responseHeaderReceived(const QHttpResponseHeader& resp);
    11. private:
    12. QHttp* http;
    13. QQueue<QFile*> files;
    14. QQueue<QFile*> started;
    To copy to clipboard, switch view to plain text mode 

    The constructor basically creates QFile*'s for files min to max. Then these are placed in the files queue. When downloadFiles() is called, the http->get() is called on each of the files, and the QFile*'s are placed in the started queue.

    When the requestFinished signal is emitted the requestFinished function is called and checks the last response header for errors. If there was an error, it deletes the file, else it closes the file and dequeues it from the started queue.

    When http::done(bool) is emitted, the connect done(bool) emits its own done() signal.

    [EDIT:] I added a request started connection and it shows that the file request is started and then it does nothing. [/EDIT]

    If you need more info or some snippets from the implementation file, please let me know.
    Last edited by last2kn0; 12th October 2007 at 21:16.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    My long shot is that you are opening too many files at once. Processes have limits for active file descriptors. You can open a file only when it's needed.

  6. #6
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    I open each file right before I call its respective QHttp::get and close and delete the pointer when its finished.

    Qt Code:
    1. void Http::downloadFiles(){
    2. while(files.size()){
    3. files.front()->open(QIODevice::WriteOnly);
    4. QFileInfo info(*files.front());
    5.  
    6. http->get("/stylepages/"+info.fileName(),files.front());
    7. started.enqueue(files.dequeue());
    8. }
    9. }
    10.  
    11.  
    12. void Http::requestFinished(int id, bool error){
    13. if(http->lastResponse().statusCode() != 200 || error){
    14. std::cout << id << " encountered an error:\n ";
    15. std::cout << http->lastResponse().statusCode() << " " <<
    16. http->lastResponse().reasonPhrase().toStdString() << "\n";
    17. started.front()->remove();
    18. delete started.dequeue();
    19. }
    20. else{
    21. started.front()->close();
    22. delete started.dequeue();
    23. std::cout << "File closed. " << id << std::endl;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    But get() calls only cause the request to be queued and not executed immediately. Thus if you do:
    Qt Code:
    1. for(int i=0;i<1000;i++)
    2. http->get(QString("/file%1").arg(i+1), new QFile(QString("file%1").arg(i+1)));
    To copy to clipboard, switch view to plain text mode 
    You end up with 1000 files open by the time when file1 gets downloaded.

    If you could show us the exact code we'd be able to say more - as I said, my opinion is a long shot, but it seems probable is you open files in advance.

  8. #8
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    The exact code is posted right above your post. If you look, I was opening the file in the front of the queue right before I called get(). I removed that line and it was able to download a lot more files but still stopped.

    Here is my implementation file:
    Qt Code:
    1. Http::Http(int min, int max, QObject* parent) : QObject(parent){
    2. //Setup http
    3. http = new QHttp("iq.ul.com");
    4. connect(http,SIGNAL(requestFinished(int,bool)),this,
    5. SLOT(requestFinished(int,bool)));
    6. connect(http,SIGNAL(done(bool)),this,SLOT(done(bool)));
    7. connect(http,SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),
    8. this,SLOT(responseHeaderReceived(const QHttpResponseHeader&)));
    9. connect(http,SIGNAL(requestStarted(int)),this,SLOT(requestStarted(int)));
    10.  
    11. //Create QFile* and place in download queue, files
    12. for(int i = min; i < max; ++i){
    13. QString newPath = "C:/Users/Desktop/UL/Update/Http/files/";
    14. QFile* temp= new QFile(newPath +QString::number(i)
    15. + "-AWM-Fixed.rtf");
    16. std::cout << "adding " << i << std::endl;
    17. files.enqueue(temp);
    18. }
    19.  
    20. }
    21.  
    22. void Http::downloadFiles(){
    23. while(files.size()){
    24.  
    25. QFileInfo info(*files.front());
    26.  
    27. http->get("/stylepages/"+info.fileName(),files.front());
    28. started.enqueue(files.dequeue());
    29. }
    30. }
    31.  
    32.  
    33. void Http::requestFinished(int id, bool error){
    34. if(http->lastResponse().statusCode() != 200 || error){
    35. std::cout << id << " encountered an error:\n ";
    36. std::cout << http->lastResponse().statusCode() << " " <<
    37. http->lastResponse().reasonPhrase().toStdString() << "\n";
    38. started.front()->remove();
    39. delete started.dequeue();
    40. }
    41. else{
    42. started.front()->close();
    43. delete started.dequeue();
    44. std::cout << "File closed. " << id << std::endl;
    45. }
    46. }
    47.  
    48. void Http::responseHeaderReceived(const QHttpResponseHeader& resp){
    49. std::cout << resp.statusCode() << " " << resp.reasonPhrase().toStdString() << std::endl;
    50. }
    51.  
    52. void Http::requestStarted(int id){
    53. std::cout << id << " started\n";
    54. }
    55.  
    56. void Http::done(bool error){
    57. if (started.size() ==0){
    58. emit done();
    59. std::cout << "Files done\n";
    60. return;
    61. }
    62. if (error){
    63. std::cout << "Error in done()\n";
    64. }
    65.  
    66.  
    67. }
    To copy to clipboard, switch view to plain text mode 

    How should I download these files if I can't do it like this??
    Last edited by last2kn0; 13th October 2007 at 00:26.

  9. #9
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    Ok, what is the best way to do this? Should I not start a new get() call until one has finished? So instead, call out to get() for the next file in the requestFinished() slot? Rather than calling them all at once?

  10. #10
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    I reimplemented the program so that there is only one QFile open at a time and I still get the same problem, although the program uses a lot less memory, which is good.

    I output whenever a request is started, when the header is received and when the request is finished. The program hangs after the request is started and never receives a header. I do not believe this is a problem with the QFiles, although that change was good.

    I don't really know what to do... if this is unavoidable is there someway to make it timeout if it doesn't get a header? Shouldn't it do this automatically?

    Thank you again.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    No, it won't timeout by itself. Does the download stop after the same number of files each time? What system are you running your application under?

  12. #12
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    No, it stops after different numbers each time. I'm am running it on Windows Vista.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    Please try running the same application on some other OS (MacOS or Linux).

  14. #14
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    I ran it on OpenSuse Linux 10.3 only to run into the same problem.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    You can try using my downloader available here: http://www.qtcentre.org/forum/f-qt-p...-qt4-5695.html (at the end of the thread). See if it works for you.

  16. #16
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttp::get seems to hang on request

    Ok, I checked out your httpdownloader and looked at the design. I then changed the main.cpp to download the files from the ul server. Your program also stalled.

    I can see why. Your program has 5 concurrent downloads so after it ran into 5 that it couldn't get a response header from it just basically stopped and waited for those files which just weren't coming.

    So does this mean its a problem with the server just not being able to provide the files?
    Last edited by last2kn0; 14th October 2007 at 19:03.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp::get seems to hang on request

    It could be that it blocks traffic if it sees a big burst of data. You can try downloading slower - delaying subsequent GETs a little. Using a technique similar to the one used by the public downlaoder I managed to successfuly download over a thousand images from a single server. If you want the code, PM me and I'll provide it to you. I don't want it to spread to public as it's not polished yet.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.