Results 1 to 1 of 1

Thread: QTcpSocket EventLoop thread is still running?

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket EventLoop thread is still running?

    Hi there,

    I am working on a solution for let say network scanner. I need to find any host existing in a subnet. What I need is to try connect to host and if connection is established signal it somehow (signal/slot mechanism is not an issue here) to main thread. This scanner should be quite fast so i'm thinking of multiple parallel connection at the same time.
    My idea is to have a collection of QTcpSockets and use it asynchronousely in main thread.
    The first problem is that there is no way (as far as i know) to set connection timeout for QTcpSocket::connectToHost() for asynchronous connection (i know there is such a function waitForConnected()). I am not sure if its a good idea but I use starttimer() to simulate this timeout. When its timed out I emit signal connectionError(SocketTimeout) and then i abort connection and try to connect to next host;
    Everythings works perfect exept one thing.
    After first call of connectToHost() one more thread i created (is that for sockets event loop?). When I try to use more than 2 QTcpSockets this new created thread remains (is still running) after application is closed. It stays in a system for quite long time.

    My question is:
    How to avoid this remaining thread after application close?
    Alternatively how to set timeout for asynchronous connection to host?
    Maybe someone has better solution for this problem?

    I'm using Qt 4.6.3 on Windows XP SP3.

    Here is minimal code reproducing the problem.

    Thanks a lot in advice.

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpSocket>
    6. #include <QHostAddress>
    7. #include <tsdevicediscoverysocket.h>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. TSDeviceDiscoverySocket sockets[2];
    24. QHostAddress hostAddress;
    25. bool stop;
    26.  
    27. public slots:
    28. void onBtnStop();
    29. void onBtnStart();
    30. void onComplete(TSDeviceDiscoverySocket*,bool);
    31.  
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(ui->btnStart, SIGNAL(pressed()), this, SLOT(onBtnStart()));
    11. connect(ui->btnStop, SIGNAL(pressed()), this, SLOT(onBtnStop()));
    12.  
    13. for(int i = 0; i < 2; i++)
    14. {
    15. connect(&sockets[i], SIGNAL(DiscoveryCompleted(TSDeviceDiscoverySocket*,bool)), this, SLOT(onComplete(TSDeviceDiscoverySocket*,bool)));
    16. }
    17.  
    18. stop = false;
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void MainWindow::onBtnStop()
    27. {
    28. stop = true;
    29. }
    30.  
    31. void MainWindow::onBtnStart()
    32. {
    33. hostAddress.setAddress("192.168.0.1");
    34.  
    35. for(int i = 0; i < 2; i++)
    36. {
    37. sockets[i].lookForHostAt(hostAddress);
    38. }
    39. }
    40.  
    41. void MainWindow::onComplete(TSDeviceDiscoverySocket* sender, bool s)
    42. {
    43. if(!stop)
    44. {
    45. hostAddress.setAddress(hostAddress.toIPv4Address() + 1);
    46. qDebug() << QString("Connecting to host %1").arg(hostAddress.toString());
    47. sender->lookForHostAt(hostAddress);
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TSDEVICEDISCOVERYSOCKET_H
    2. #define TSDEVICEDISCOVERYSOCKET_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QHostAddress>
    6.  
    7.  
    8. class TSDeviceDiscoverySocket : public QTcpSocket
    9. {
    10. Q_OBJECT
    11. private:
    12. int _timeout;
    13. int _currTimerId;
    14.  
    15. public:
    16. explicit TSDeviceDiscoverySocket(QObject *parent = 0);
    17. void lookForHostAt(const QHostAddress &ip);
    18.  
    19. protected:
    20. void timerEvent(QTimerEvent *);
    21.  
    22. signals:
    23. void DiscoveryCompleted(TSDeviceDiscoverySocket* sender, bool succeeded);
    24. void DiscoveryStarted(const TSDeviceDiscoverySocket* sender, const QHostAddress &address);
    25.  
    26.  
    27. //public slots:
    28. private slots:
    29. void onConnectedToHost();
    30. void onError(QAbstractSocket::SocketError);
    31. };
    32.  
    33. #endif // TSDEVICEDISCOVERYSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "tsdevicediscoverysocket.h"
    2.  
    3. TSDeviceDiscoverySocket::TSDeviceDiscoverySocket(QObject *parent) :
    4. QTcpSocket(parent)
    5. {
    6. connect(this, SIGNAL(connected()), this, SLOT(onConnectedToHost()));
    7. connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
    8.  
    9. _timeout = 3000;
    10. }
    11.  
    12.  
    13. void TSDeviceDiscoverySocket::timerEvent(QTimerEvent *te)
    14. {
    15. Q_UNUSED(te);
    16. onError(QAbstractSocket::SocketTimeoutError);
    17. }
    18.  
    19. void TSDeviceDiscoverySocket::lookForHostAt(const QHostAddress &ip)
    20. {
    21. emit DiscoveryStarted(this, ip);
    22. _currTimerId = startTimer(_timeout);
    23. connectToHost(ip, 2376);
    24. }
    25.  
    26. void TSDeviceDiscoverySocket::onConnectedToHost()
    27. {
    28. killTimer(_currTimerId);
    29. emit DiscoveryCompleted(this, true);
    30. }
    31.  
    32. void TSDeviceDiscoverySocket::onError(QAbstractSocket::SocketError e)
    33. {
    34. killTimer(_currTimerId);
    35. abort();
    36. emit DiscoveryCompleted(this, false);
    37. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. Replies: 1
    Last Post: 28th January 2010, 09:23
  2. Replies: 16
    Last Post: 7th October 2009, 08:17
  3. is qtcpsocket a thread
    By ersin.ozkan in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2009, 09:39
  4. Thread eventLoop and run
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:36
  5. running() - Thread
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2007, 18:45

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.