Results 1 to 13 of 13

Thread: Networking problems while off-line

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Networking problems while off-line

    Gentlemen,
    I posted a huge problem some time ago and I assume it was too long.
    I made some examinations and I am pretty sure that it has the same roots as the situation I describe below.

    PROBLEM
    Let us have a program that uses QFtp to download some file.
    This applies to the program below:
    Qt Code:
    1. #ifndef FTPGET_H
    2. #define FTPGET_H
    3.  
    4. #include <QFile>
    5. #include <QFtp>
    6.  
    7. class QUrl;
    8.  
    9. class FtpGet : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. FtpGet(QObject *parent = 0);
    15.  
    16. bool getFile(const QUrl &url);
    17.  
    18. signals:
    19. void done();
    20.  
    21. private slots:
    22. void ftpDone(bool error);
    23.  
    24. private:
    25. QFtp ftp;
    26. QFile file;
    27. };
    28.  
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <iostream>
    4.  
    5. #include "ftpget.h"
    6.  
    7. using namespace std;
    8.  
    9. FtpGet::FtpGet(QObject *parent)
    10. : QObject(parent)
    11. {
    12. connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
    13. }
    14.  
    15. bool FtpGet::getFile(const QUrl &url)
    16. {
    17. if (!url.isValid()) {
    18. cerr << "Error: Invalid URL" << endl;
    19. return false;
    20. }
    21.  
    22. if (url.scheme() != "ftp") {
    23. cerr << "Error: URL must start with 'ftp:'" << endl;
    24. return false;
    25. }
    26.  
    27. if (url.path().isEmpty()) {
    28. cerr << "Error: URL has no path" << endl;
    29. return false;
    30. }
    31.  
    32. QString localFileName = QFileInfo(url.path()).fileName();
    33. if (localFileName.isEmpty())
    34. localFileName = "ftpget.out";
    35.  
    36. file.setFileName(localFileName);
    37. if (!file.open(QIODevice::WriteOnly)) {
    38. cerr << "Error: Cannot open " << qPrintable(file.fileName())
    39. << " for writing: " << qPrintable(file.errorString())
    40. << endl;
    41. return false;
    42. }
    43.  
    44. ftp.connectToHost(url.host(), url.port(21));
    45. ftp.login();
    46. ftp.get(url.path(), &file);
    47. ftp.close();
    48. return true;
    49. }
    50.  
    51. void FtpGet::ftpDone(bool error)
    52. {
    53. if (error) {
    54. cerr << "Error: " << qPrintable(ftp.errorString()) << endl;
    55. } else {
    56. cerr << "File downloaded as " << qPrintable(file.fileName())
    57. << endl;
    58. }
    59. file.close();
    60. emit done();
    61. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. #include "ftpget.h"
    5.  
    6. using namespace std;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication app(argc, argv);
    11. QStringList args = app.arguments();
    12.  
    13. if (args.count() != 2) {
    14. cerr << "Usage: ftpget url" << endl
    15. << "Example:" << endl
    16. << " ftpget ftp://ftp.trolltech.com/mirrors" << endl;
    17. return 1;
    18. }
    19.  
    20. FtpGet getter;
    21. if (!getter.getFile(QUrl(args[1])))
    22. return 1;
    23.  
    24. QObject::connect(&getter, SIGNAL(done()), &app, SLOT(quit()));
    25.  
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    This is an example application from one of the QT devoted books.

    It runs smoothly when the user is on-line.

    The problem arises when the user has some connection problems.
    Try to run the application while off-line.
    The console becomes blocked and it will be until we connect ourselves to web or kill the process.

    My question:
    How to make our application quit with a certain warning for example after 10 seconds of unsuccessful trials to connect to the web?

    PS. I tried to use QFtp::abort() with QTimer but it did not work!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    You're not using QFtp properly. I assume the application blocks in QFtp::get.
    The states of QFtp::connectToHost are HostLookup, Connecting and Connected. When offline the state will remain HostLookup. But you do not test for this(the stateChanged signal), you just go ahead and login(what I said about connectToHost also applies here) and then download the file.

    The correct way is to examine all states and connect to all relevant signals in order to track possible connection errors.

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

    Default Re: Networking problems while off-line

    Try connecting to the stateChanged() signal and reporting any changes through qDebug(). This will at least tell you what happens with the ftp object.

  4. #4
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    You're not using QFtp properly. I assume the application blocks in QFtp::get.
    No. The application blocks on HostLookup as I am offline!
    The problem is that I want to break host lookup after 15 seconds of that state.

    I tried something like that:
    Qt Code:
    1. void MyWidget::connectionManager(int state) {
    2. if(state == QFtp::HostLookup) {
    3. qDebug("QFtp::HostLookup");
    4. timer.setInterval(1000);
    5. connect(&timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));
    6. seconds = 0;
    7. timer.start();
    8. }
    9. }
    10. void MyWidget::timerTimeOut() {
    11. ++seconds;
    12. if(seconds > 6) {
    13. qDebug("Connecting stopped.");
    14. ftp->abort();
    15. ftp->close();
    16. timer.stop();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 


    where
    Qt Code:
    1. connect(ftp,SIGNAL(stateChanged(int)),this,SLOT(connectionManager(int)));
    To copy to clipboard, switch view to plain text mode 

    but still I have a problem with quiting my application.
    It freezes after clicking exit button and quits properly only if I plug the internet cable and thus revive connection.
    This is why I suspect networking as a reason of the problem.

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    It freezes after clicking exit button and quits properly only if I plug the internet cable and thus revive connection.
    Do you also abort the ftp in closeEvent?

    Also, how many times do you get the HostLookup state?

  6. #6
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    Quote Originally Posted by marcel View Post
    Do you also abort the ftp in closeEvent?
    Qt Code:
    1. void MyWidget::closeEvent(QCloseEvent *event) {
    2. ftp->abort();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Also, how many times do you get the HostLookup state?
    Once

  7. #7
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Networking problems while off-line

    Just a thought but have tried using QNetworkInterface to determine if you have a network connection before trying to connect. Requires Qt 4.2 though.

    Pete

  8. #8
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    How to do this?
    I looked at the documentation but couldn't sketch the code.
    http://doc.trolltech.com/4.2/qnetworkinterface.html

    I assume that QFtp::abort() is designed to work well as a tool for stopping the download. The whole issue is that I want to know how to order QFtp to stop looking for host as this blocks destructor of main application and I am frozen.

    Moreover as far as I understand if QFtp fails in finding host it should signal done(true) and set error as QFtp::HostNotFound...

    Is it a bug?
    Last edited by mtrpoland; 26th September 2007 at 21:29. Reason: additional information

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

    Default Re: Networking problems while off-line

    I don't understand one thing... Do you say that anything works incorrectly when you call abort()? Because I haven't seen you point out anything incorrect in QFtp behaviour... You call abort() and then what? It seems that you immediately call close() which schedules a disconnection and prevents done() from firing (see QFtp::done() docs for details) which prevents your application from closing. Close() will it turn do nothing as there is no live connection and again done() will probably not get emitted. Is it in any way different from what you expected?

  10. #10
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    I have written a tiny program that presents solely the area of my concern.
    Compile it and run it twice.
    First with connection.
    Second time disconnect yourself from internet before running the application and plug the cable after the application freezes.
    main.cpp
    Qt Code:
    1. #include "test.hpp"
    2. #include <QApplication>
    3. #include <QWidget>
    4.  
    5. int main(int argc, char * argv[]) {
    6. QApplication app(argc, argv);
    7. TestDialog widget;
    8. widget.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    test.hpp
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3. #include <QDialog>
    4. class QFtp;
    5.  
    6. class TestDialog : public QDialog {
    7. Q_OBJECT
    8. public:
    9. TestDialog(QWidget *parent = 0);
    10. ~TestDialog();
    11. public slots:
    12. void done(bool);
    13. private:
    14. QPushButton *pbClose;
    15. QFtp *ftp;
    16. };
    17. #endif
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include "test.hpp"
    2. #include <QApplication>
    3. #include <QtDebug>
    4. #include <QPushButton>
    5. #include <QFtp>
    6. #include <QVBoxLayout>
    7.  
    8. TestDialog::TestDialog(QWidget *parent) : QDialog(parent) {
    9. QVBoxLayout *qvb = new QVBoxLayout();
    10. pbClose = new QPushButton("Close");
    11. ftp = new QFtp(this);
    12. qvb->addWidget(pbClose);
    13. setLayout(qvb);
    14. connect(pbClose,SIGNAL(clicked()),qApp,SLOT(quit()));
    15. connect(ftp,SIGNAL(done(bool)),this,SLOT(done(bool)));
    16. ftp->connectToHost("ftp://ftp.mjakobczyk.pl");
    17. }
    18.  
    19. TestDialog::~TestDialog() {
    20. ftp->abort();
    21. qDebug("Destructor");
    22. }
    23.  
    24. void TestDialog::done(bool bl) {
    25. qDebug() << "Done" << bl;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for support.
    Mike
    Last edited by mtrpoland; 27th September 2007 at 10:02.

  11. #11
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Networking problems while off-line

    I tried your code, but never got any blocking behaviour unplugging the cable. However, here's a method I tried that did detect if I had any IPV4 addresses not equal to 127.0.0.1. I'm sure its not the most elegant solution, but its the type of thing I was alluding to earlier. I'm also sure the Qt gurus may have other comments, but its the best I can do for a 50 year old FORTRAN programmer.

    This was using Qt4 4.3.1 under VS2005.

    test.hpp
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3. #include <QDialog>
    4. class QFtp;
    5.  
    6. class TestDialog : public QDialog {
    7. Q_OBJECT
    8. public:
    9. TestDialog(QWidget *parent = 0);
    10. ~TestDialog();
    11. bool onLine();
    12. public slots:
    13. void done(bool);
    14. private:
    15. QPushButton *pbClose;
    16. QFtp *ftp;
    17. };
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include "test.hpp"
    2. #include <QApplication>
    3. #include <QtDebug>
    4. #include <QPushButton>
    5. #include <QFtp>
    6. #include <QVBoxLayout>
    7. #include <QNetworkInterface>
    8. #include <QHostAddress>
    9.  
    10. TestDialog::TestDialog(QWidget *parent) : QDialog(parent), ftp(0) {
    11. QVBoxLayout *qvb = new QVBoxLayout();
    12. pbClose = new QPushButton("Close");
    13. qvb->addWidget(pbClose);
    14. setLayout(qvb);
    15. connect(pbClose,SIGNAL(clicked()),qApp,SLOT(quit()));
    16. if (onLine()) {
    17. ftp = new QFtp(this);
    18. connect(ftp,SIGNAL(done(bool)),this,SLOT(done(bool)));
    19. ftp->connectToHost("ftp://ftp.mjakobczyk.pl");
    20. } else {
    21. pbClose->setText("No IP Address");
    22. }
    23.  
    24. }
    25.  
    26. TestDialog::~TestDialog() {
    27. if (ftp) {
    28. ftp->abort();
    29. }
    30. qDebug("Destructor");
    31. }
    32.  
    33. void TestDialog::done(bool bl) {
    34. qDebug() << "Done" << bl;
    35. }
    36.  
    37. bool TestDialog::onLine() {
    38. QList<QHostAddress> ads = QNetworkInterface::allAddresses();
    39. if (ads.size() > 0) {
    40. for (int i = 0; i < ads.size(); i++) {
    41. quint32 ipv4 = ads.at(i).toIPv4Address();
    42. qDebug() << ipv4;
    43. //Any address != loopback
    44. if (ipv4 > 0 && ipv4 != 2130706433 /* == 127.0.0.1*/) {
    45. return true;
    46. }
    47. }
    48. }
    49. return false;
    50. }
    To copy to clipboard, switch view to plain text mode 

    Pete
    Last edited by pdolbey; 27th September 2007 at 19:49. Reason: Platform clarified

  12. The following user says thank you to pdolbey for this useful post:

    mtrpoland (28th September 2007)

  13. #12
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Networking problems while off-line

    Thanks for the code.
    I have discovered that my code works perfectly on windows xp, so the behavior I had described before occurs only on my ubuntu linux.

  14. #13
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Networking problems while off-line

    I've the same problem in Kubuntu... have you tested in debian? occurs the same problem?

    Thanks!

Similar Threads

  1. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  2. Access Violation on Signal Emit
    By khagzan in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2007, 22:51
  3. QGLWidget bug
    By Wizard in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2007, 11:23
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.