Results 1 to 9 of 9

Thread: QTcpSocket deamon-thread

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

    Default QTcpSocket deamon-thread

    Hi there,

    i am programming QTcpSockets and i've met some strange behaviour. I run my sockets in main thread and connect them with signals and slots. It seems than QTcpSocket class create some kind of internal deamon-thread. The problem starts when i create let say more than 10 sockets and try connect to host. After they timeout the deamon-thread remains in the system. Even when i close whole app the process (1 thread) stays for quite long time. More sockets - longer time.

    How to avoid this long lasting deamon-thread?
    alternatively how to terminate deamon-thread on app close?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket deamon-thread

    strange behaviour! can you post code?
    A camel can go 14 days without drink,
    I can't!!!

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

    Default Re: QTcpSocket deamon-thread

    Here is code ilustrating the problem:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpSocket>
    6. #include <QHostAddress>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22. static const int numOfSockets = 50;
    23. QTcpSocket sockets[numOfSockets];
    24. QHostAddress hostAddress;
    25. int numOfProccessing;
    26.  
    27.  
    28. public slots:
    29. void onBtnStart();
    30. void onError(QAbstractSocket::SocketError);
    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.  
    12. for(int i = 0; i < MainWindow::numOfSockets; i++)
    13. {
    14. connect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
    15. }
    16.  
    17. ui->btnStart->setText(QString("Start(%1)").arg(MainWindow::numOfSockets));
    18. hostAddress.setAddress("192.168.0.1");
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. for(int i = 0; i < MainWindow::numOfSockets; i++)
    24. {
    25. disconnect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
    26.  
    27. if(sockets[i].isOpen())
    28. {
    29. sockets[i].close();
    30. }
    31. }
    32.  
    33. delete ui;
    34. }
    35.  
    36.  
    37. void MainWindow::onBtnStart()
    38. {
    39. numOfProccessing = 0;
    40. ui->btnStart->setEnabled(false);
    41.  
    42. for(int i = 0; i < MainWindow::numOfSockets; i++)
    43. {
    44. hostAddress.setAddress(hostAddress.toIPv4Address() + 1);
    45. sockets[i].connectToHost(hostAddress, 2376);
    46. qDebug() << QString("connect to host %1").arg(hostAddress.toString());
    47. numOfProccessing++;
    48. }
    49. }
    50.  
    51.  
    52. void MainWindow::onError(QAbstractSocket::SocketError e)
    53. {
    54. ui->btnStart->setEnabled(--numOfProccessing == 0);
    55.  
    56. qDebug() << QString("MainWindow::onError %1").arg(e);
    57. }[ATTACH]6016[/ATTACH]
    To copy to clipboard, switch view to plain text mode 


    Added after 1 47 minutes:


    It seems that these sockets exist even after app was closed. I used Wireshark to check if there is any traffic after sockets were deleted and it was. Can anybody tell me how to permanently and immediately close these sockets? QTcpSocket::abort() does not the trick.
    Last edited by mihau; 2nd March 2011 at 08:31.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTcpSocket deamon-thread

    Hi mihau
    I've just downloaded your code, looks like I can't reproduce this problem - after closing the app, all TCP traffic is gone ( checked with Wireshark as well, using Windows XP SP3 ).
    How did you checked for the existence of daemon-thread ? For me (should say ProcessExplorer) it looks like all threads spawned by app are removed at exit.
    Can you reproduce this behaviour on different machines ?

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

    Default Re: QTcpSocket deamon-thread

    I checked for thread existence in ProcessExplorer as well. I've tried to run this code on different machines and it seems to be machine dependent problem. Thanks for point out stampede.
    Is it network driver fault? Any ideas?

  6. #6
    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: QTcpSocket deamon-thread

    What "daemon thread" do you mean exactly? QTcpSocket doesn't spawn any external processes. Nor threads, for that matter.
    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.


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

    Default Re: QTcpSocket deamon-thread

    "Daemon thread" is just an expression. I don't know exacly what is happening. On some machines one thread lasts even after application was closed and network traffic is still present. It seems that these sockets still exist in system. In some cases this situation can lead to bluescreen!

  8. #8
    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: QTcpSocket deamon-thread

    Quote Originally Posted by mihau View Post
    "Daemon thread" is just an expression.
    Well, if you didn't know the road traffic rules and somebody told you that you should walk into the road when the red light is on and when you do that and get ran over by a car, I don't think you'd agree that "walk into the road when the red light is on" was just an expression.

    On some machines one thread lasts even after application was closed and network traffic is still present. It seems that these sockets still exist in system. In some cases this situation can lead to bluescreen!
    Do you mean 'thread' or 'just-an-expression-thread'? What lasts where? Could you be more specific?
    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
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket deamon-thread

    A process (including one thread) exists in ProcessExplorer after app was closed. When network traffic is over this process disapears. I've tested it on several machines (all with Win XP) and this behaviour could not be reproduced on every one.

Similar Threads

  1. QTcpSocket EventLoop thread is still running?
    By mihau in forum Qt Programming
    Replies: 0
    Last Post: 28th February 2011, 09:41
  2. Replies: 5
    Last Post: 22nd February 2011, 21:21
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Replies: 16
    Last Post: 7th October 2009, 08:17
  5. is qtcpsocket a thread
    By ersin.ozkan in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2009, 09:39

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.