PDA

View Full Version : QTcpSocket deamon-thread



mihau
1st March 2011, 10:27
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?

mcosta
1st March 2011, 18:05
strange behaviour! can you post code?

mihau
2nd March 2011, 09:31
Here is code ilustrating the problem:



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
static const int numOfSockets = 50;
QTcpSocket sockets[numOfSockets];
QHostAddress hostAddress;
int numOfProccessing;


public slots:
void onBtnStart();
void onError(QAbstractSocket::SocketError);

};

#endif // MAINWINDOW_H




#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

connect(ui->btnStart, SIGNAL(pressed()), this, SLOT(onBtnStart()));

for(int i = 0; i < MainWindow::numOfSockets; i++)
{
connect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
}

ui->btnStart->setText(QString("Start(%1)").arg(MainWindow::numOfSockets));
hostAddress.setAddress("192.168.0.1");
}

MainWindow::~MainWindow()
{
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
disconnect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));

if(sockets[i].isOpen())
{
sockets[i].close();
}
}

delete ui;
}


void MainWindow::onBtnStart()
{
numOfProccessing = 0;
ui->btnStart->setEnabled(false);

for(int i = 0; i < MainWindow::numOfSockets; i++)
{
hostAddress.setAddress(hostAddress.toIPv4Address() + 1);
sockets[i].connectToHost(hostAddress, 2376);
qDebug() << QString("connect to host %1").arg(hostAddress.toString());
numOfProccessing++;
}
}


void MainWindow::onError(QAbstractSocket::SocketError e)
{
ui->btnStart->setEnabled(--numOfProccessing == 0);

qDebug() << QString("MainWindow::onError %1").arg(e);
}6016


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.

stampede
2nd March 2011, 10:23
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 ?

mihau
2nd March 2011, 12:12
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?

wysota
2nd March 2011, 14:23
What "daemon thread" do you mean exactly? QTcpSocket doesn't spawn any external processes. Nor threads, for that matter.

mihau
17th March 2011, 15:55
"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!

wysota
18th March 2011, 00:50
"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?

mihau
18th March 2011, 08:58
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.