Results 1 to 11 of 11

Thread: Internet Speedometer

  1. #1
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Internet Speedometer

    Hello,
    I'm new in QT. I have to implement internet speedometer. The application must have the following properties:
    1) To work synchronous - with threads;
    2) To receive parameters: IP address of server, port and interval (ms), in which we make ping to the server;
    3) Use Dial or progressBar for ping time connection status;
    4) To watch for connection failure with announcement;
    5) To gather information for connection and on request to give average results.

    I made a review of QtcpSocket class and the examples relevant to it. I've made the .ui file. But I'm hard up for an answer to make the rest of an application. Can someone give me an advice in basic steps how to implement it.
    Thank you in advance.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Internet Speedometer

    thats a lot of problems..target them one by one....i cant imagine a coherent reply to that post..so whats the first problem?

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

    Default Re: Internet Speedometer

    I'd start with "why do want a synchronous solution?". Being synchronous makes your application much more complex without any benefits on its own. Unless that is an outside requirement I would change the design to using asynchronous calls.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Internet Speedometer

    I'll try to explain in details what I must do. I have big application, working with DB. When I have some request to this database, I would like to check the connection to the server, to be sure that I have a responce from the server or to understand where is the problem of slow connection.
    So if you still think that I should do it asynchronous, I'm waitnig for your suggestions.

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

    Default Re: Internet Speedometer

    Yes, I think it should be asynchronous You start a call, you wait until you have a response (or not) and then you make a call to the database. Just a side note - if you really want to "ping" the server, getting no response won't mean there is no connection - the pings might be blocked. The best option would be to try to connect to the database regardless of the reachability of the remote site. The connection will simply fail (QSqlDatabase::open will return false).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Internet Speedometer

    Thanks for the help. While I was waiting for your answer, I tried to make the app synchronous. And now I have a problem while executing it.When I push the Start button in a few seconds later I receive the following message: "The following error occurred: Socket operation timeout."

    I will post here the source code:

    1.speedometerthread.h
    Qt Code:
    1. #ifndef SPEEDOMETERTHREAD_H
    2. #define SPEEDOMETERTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6. #include <QWaitCondition>
    7.  
    8. class SpeedometerThread : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. SpeedometerThread(QObject *parent = 0);
    14. ~SpeedometerThread();
    15.  
    16. void requestNewSpeedometer(const QString &hostName, quint16 port); // to request a new speedometer,
    17. //and the result is delivered by the
    18. //newSpeedometer() signal
    19. void run();
    20.  
    21. signals:
    22. void newSpeedometer(const QString &speedometer); //The reault of invoking the requestNewSpeedometer()
    23. void error(int socketError, const QString &message); //If any error occurs, the error() signal is emitted.
    24.  
    25. private:
    26. QString hostName; //requestNewSpeedometer() is called from the main, GUI thread, but the host name and port
    27. quint16 port; //values it stores will be accessed from SpeedometerThread's thread.
    28. QMutex mutex; // Because we will be reading and writing FortuneThread's data members from different threads
    29. //concurrently, we use QMutex to synchronize access.
    30. bool quit;
    31. };
    32.  
    33. #endif // SPEEDOMETERTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    2. speedometerthread.cpp
    Qt Code:
    1. #include <QtNetwork>
    2.  
    3. #include "speedometerthread.h"
    4.  
    5. SpeedometerThread::SpeedometerThread(QObject *parent)
    6. : QThread(parent), quit(false)
    7. {
    8. }
    9.  
    10. SpeedometerThread::~SpeedometerThread()
    11. {
    12. mutex.lock();
    13. quit = true; // sets quit to true
    14. cond.wakeOne(); // wakes up the thread
    15. mutex.unlock();
    16. wait(); //waits for the thread to exit before returning. This lets the while loop in run() will finish
    17. // its current iteration. When run() returns, the thread will terminate and be destroyed.
    18. }
    19.  
    20. void SpeedometerThread::requestNewSpeedometer(const QString &hostName, quint16 port)
    21. {
    22. QMutexLocker locker(&mutex); // stores the host name and port of the fortune server as member data, and we
    23. this->hostName = hostName; // lock the mutex with QMutexLocker to protect this data. We then start the
    24. this->port = port; //thread, unless it is already running.
    25. if (!isRunning())
    26. start();
    27. else
    28. cond.wakeOne(); // because the thread falls asleep waiting for a new request, we needed to wake it up
    29. // again when a new request arrives. QWaitCondition is often used in threads to
    30. // signal a wakeup call like this.
    31. }
    32.  
    33. void SpeedometerThread::run()
    34. {
    35. mutex.lock(); // acquiring the mutex lock
    36. QString serverName = hostName; // fetching the host name and port from the member data
    37. quint16 serverPort = port;
    38. mutex.unlock(); // releasing the lock again
    39.  
    40. while (!quit)
    41. {
    42. const int Timeout = 5 * 1000; // (in ms)
    43.  
    44. QTcpSocket socket; //creating a QTcpSocket on the stack
    45. socket.connectToHost(serverName, serverPort); // starts an asynchronous operation which, after control
    46. // returns to Qt's event loop, will cause QTcpSocket
    47. // to emit connected() or error().
    48. if (!socket.waitForConnected(Timeout)) //since we are running in a non-GUI thread, we do not have to worry
    49. { // about blocking the user interface. So instead of entering an event loop, we simply call
    50. // QTcpSocket::waitForConnected(). This function will wait, blocking the calling thread,
    51. //until QTcpSocket emits connected() or an error occurs. If connected() is emitted, the function returns
    52. // true; if the connection failed or timed out (which in this example happens after 5 seconds),
    53. // false is returned. QTcpSocket::waitForConnected(), like the other waitFor...() functions, is part of
    54. //QTcpSocket's blocking API.
    55. emit error(socket.error(), socket.errorString());
    56. return;
    57. }
    58.  
    59. while (socket.bytesAvailable() < (int)sizeof(quint16))
    60. { // we have a connected socket to work with. Now it's time to see what the fortune server has sent us.
    61. if (!socket.waitForReadyRead(Timeout)) // read the size of the packet. Although we are only reading
    62. { // two bytes here, and the while loop may seem to overdo it, waiting for data using
    63. // QTcpSocket::waitForReadyRead(). For as long as we still need more data, we call waitForReadyRead().
    64. // If it returns false, we abort the operation. After this statement, we know that we
    65. //have received enough data.
    66. emit error(socket.error(), socket.errorString());
    67. return;
    68. }
    69. }
    70.  
    71. quint16 blockSize;
    72. QDataStream in(&socket); // create a QDataStream object, passing the socket to QDataStream's constructor,
    73. in.setVersion(QDataStream::Qt_4_0); // and set the stream protocol version to QDataStream::Qt_4_0, and read
    74. in >> blockSize; // the size of the packet.
    75.  
    76. while (socket.bytesAvailable() < blockSize)
    77. { //a loop that waits for more data by calling QTcpSocket::waitForReadyRead(). In this loop, we're waiting
    78. // until QTcpSocket::bytesAvailable() returns the full packet size.
    79. if (!socket.waitForReadyRead(Timeout))
    80. {
    81. emit error(socket.error(), socket.errorString());
    82. return;
    83. }
    84. }
    85.  
    86. mutex.lock();
    87. QString speedometer;
    88. in >> speedometer; //use QDataStream to read the fortune string from the packet. The resulting
    89. emit newSpeedometer(speedometer); // speedometer is delivered by emitting newSpeedometer().
    90.  
    91. cond.wait(&mutex); // acquire the mutex so that we can safely read from our member data.
    92. serverName = hostName; // We then let the thread go to sleep by calling QWaitCondition::wait().
    93. serverPort = port;
    94. mutex.unlock();
    95. }
    96. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Internet Speedometer

    3. speedometerctrlform.h
    Qt Code:
    1. #ifndef SPEEDOMETERCTRLFORM_H
    2. #define SPEEDOMETERCTRLFORM_H
    3.  
    4. #include <QtGui/QWidget>
    5.  
    6. #include "speedometerthread.h"
    7.  
    8. namespace Ui
    9. {
    10. class speedometerCtrlFormClass;
    11. }
    12.  
    13. class speedometerCtrlForm : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. speedometerCtrlForm(QWidget *parent = 0);
    19. ~speedometerCtrlForm();
    20.  
    21. private slots:
    22. void requestNewSpeedometer();
    23. void showSpeedometer(const QString &speedometer);
    24. void displayError(int socketError, const QString &message);
    25. void displayAverageValues(void);
    26.  
    27. private:
    28. Ui::speedometerCtrlFormClass *ui;
    29. SpeedometerThread thread;
    30. QString currentSpeedometer;
    31. };
    32.  
    33. #endif // SPEEDOMETERCTRLFORM_H
    To copy to clipboard, switch view to plain text mode 

    4. speedometerctrlform.cpp
    Qt Code:
    1. #include "speedometerctrlform.h"
    2. #include "ui_speedometerctrlform.h"
    3.  
    4. #include <QtGui>
    5. #include <QtNetwork>
    6.  
    7. speedometerCtrlForm::speedometerCtrlForm(QWidget *parent)
    8. : QWidget(parent), ui(new Ui::speedometerCtrlFormClass)
    9. {
    10. ui->setupUi(this);
    11.  
    12. connect(ui->startCtrlFormBtn, SIGNAL(clicked()),
    13. this, SLOT(requestNewSpeedometer()));
    14. connect(ui->CtrlFormAverageValuesBtn, SIGNAL(clicked()),
    15. this, SLOT(displayAverageValues()));
    16.  
    17. connect(&thread, SIGNAL(newSpeedometer(const QString &)),
    18. this, SLOT(showSpeedometer(const QString &)));
    19. connect(&thread, SIGNAL(error(int, const QString &)),
    20. this, SLOT(displayError(int, const QString &)));
    21.  
    22. }
    23.  
    24. speedometerCtrlForm::~speedometerCtrlForm()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void speedometerCtrlForm::requestNewSpeedometer()
    30. {
    31. ui->startCtrlFormBtn->setEnabled(false); // enables the button
    32. thread.requestNewSpeedometer("www.abv.bg", //hostLineEdit->text(),
    33. 8080);//portLineEdit->text().toInt());
    34. }
    35.  
    36. void speedometerCtrlForm::showSpeedometer(const QString &nextSpeedometer)
    37. {
    38. if (nextSpeedometer == currentSpeedometer)
    39. {
    40. requestNewSpeedometer();
    41. return;
    42. }
    43.  
    44. currentSpeedometer = nextSpeedometer;
    45. //ui->statusLabel->setText(currentSpeedometer);
    46. ui->startCtrlFormBtn->setEnabled(true);
    47. }
    48.  
    49. void speedometerCtrlForm::displayError(int socketError, const QString &message)
    50. {
    51. switch (socketError) {
    52. case QAbstractSocket::HostNotFoundError:
    53. QMessageBox::information(this, tr("Speedometer - Control Form"),
    54. tr("The host was not found. Please check the "
    55. "host and port settings."));
    56. break;
    57. case QAbstractSocket::ConnectionRefusedError:
    58. QMessageBox::information(this, tr("Speedometer - Control Form"),
    59. tr("The connection was refused by the peer. "
    60. "Make sure the speedometer server is running, "
    61. "and check that the host name and port "
    62. "settings are correct."));
    63. break;
    64. default:
    65. QMessageBox::information(this, tr("Speedometer - Control Form"),
    66. tr("The following error occurred: %1.")
    67. .arg(message));
    68. }
    69.  
    70. ui->startCtrlFormBtn->setEnabled(true);
    71. }
    72.  
    73. void speedometerCtrlForm::displayAverageValues(void)
    74. {
    75. }
    To copy to clipboard, switch view to plain text mode 

    5. main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "speedometerctrlform.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. speedometerCtrlForm speedometerFrom;
    8. speedometerFrom.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Where am I wrong?

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

    Default Re: Internet Speedometer

    The socket didn't manage to complete the requested operation within the time frame you gave it. By the way, you don't need that mutex there. At least not it its current form. You can use QReadLocker and QWriteLocker or better yet change the way you pass data to the thread and get rid of the mutex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Internet Speedometer

    Here is something quick, dirty and working (and asynchronous).
    Attached Files Attached Files
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    dilidpan (7th April 2009)

  11. #10
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Internet Speedometer

    Thanks a lot.

  12. #11
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Internet Speedometer

    Quote Originally Posted by wysota View Post
    Here is something quick, dirty and working (and asynchronous).
    hello,

    i tried to comoiple your main.cpp app. and i get many errors, and the first one is QtNetwork not found. any hints? how the .pro file should be??

    Thanks a lot

Similar Threads

  1. Hello Qt Centre
    By sumsin in forum General Discussion
    Replies: 275
    Last Post: 21st June 2020, 12:14
  2. how to create internet explorer toolbar using Qt?
    By live_07 in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2011, 07:28
  3. is myspace the paris hilton on the internet?
    By jshore in forum General Discussion
    Replies: 1
    Last Post: 20th February 2007, 06:19
  4. Check connecting to Internet
    By gtthang in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2006, 17:13
  5. http trouble again, segfaults when no internet
    By Bojan in forum Qt Programming
    Replies: 6
    Last Post: 18th January 2006, 20:25

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.