PDA

View Full Version : Problem with QTcpSocket in QThread



Raistlin
4th October 2007, 16:39
When I have a QThread which is responsible for handling a connection for a client application, I seem to have a strange problem. When I allocate a QTcpSocket in the constructor of the QThread, I do not succeed to open a connection afterwards in run(). When I do the allocation in run(), no problems arises. Any idea what might cause this ?

Header file :


#ifndef CONNECTIONTHREAD_H
#define CONNECTIONTHREAD_H

#include <QThread>
#include <QString>

class QTcpSocket;

class ConnectionThread : public QThread
{
Q_OBJECT

public:
ConnectionThread(QObject *parent);
~ConnectionThread();

bool isConnected() const;

signals:
void connectionEstablished();

public slots:
//void setHostInfo(QString& hostName, int portNumber);

private:
void run();

QTcpSocket* tcpSocket_;
bool connected_;
QString hostName_;
int portNumber_;

};

#endif // CONNECTIONTHREAD_H


This works :


#include "connectionthread.h"
#include <QTcpSocket>

ConnectionThread::ConnectionThread(QObject *parent)
: QThread(parent)
{
connected_ = false;

hostName_ = "localhost";
portNumber_ = 4974;

connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
}

ConnectionThread::~ConnectionThread()
{
}

bool ConnectionThread::isConnected() const
{
return connected_;
}

void ConnectionThread::run()
{
tcpSocket_ = new QTcpSocket(this);

tcpSocket_->abort();
tcpSocket_->connectToHost(hostName_, portNumber_);

if (tcpSocket_->waitForConnected(1000))
emit connectionEstablished();

exec();
}



This does not :


#include "connectionthread.h"
#include <QTcpSocket>

ConnectionThread::ConnectionThread(QObject *parent)
: QThread(parent)
{
connected_ = false;

tcpSocket_ = new QTcpSocket(this);

hostName_ = "localhost";
portNumber_ = 4974;

connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
}

ConnectionThread::~ConnectionThread()
{
}

bool ConnectionThread::isConnected() const
{
return connected_;
}

void ConnectionThread::run()
{
tcpSocket_->abort();
tcpSocket_->connectToHost(hostName_, portNumber_);

if (tcpSocket_->waitForConnected(1000))
emit connectionEstablished();

exec();
}

marcel
4th October 2007, 16:49
I think the socket is created in the parent thread's context.
Try calling connectionThread->moveToThread(connectionThread) from wherever you start the connection thread, but after it has started.

jpn
4th October 2007, 17:19
Are you sure you need a separate thread?

http://www.qtcentre.org/forum/f-qt-programming-2/t-problems-with-qthread-and-qtcpsocket-4242.html

Raistlin
5th October 2007, 08:13
Are you sure you need a separate thread?

http://www.qtcentre.org/forum/f-qt-programming-2/t-problems-with-qthread-and-qtcpsocket-4242.html

It might be a similar title, but I did not see the link at first. I think more trivial questions have been asked more than one time in the past :)

jpn
5th October 2007, 08:54
If you would have bothered reading the thread, you would know that

QThread constructor and QThread::run() are executed in different threads
QThread object itself lives in the thread where it was created
You shouldn't pass "this" as parent in QThread::run() because "this" lives in another thread

;)

marcel
5th October 2007, 09:00
Kids these days... They don't read anything, just want to give them the code.

Raistlin
6th October 2007, 12:11
I did not find the link first, so I was not aware of this. I understood the issue after reading the thread, and did not ask any additional things. So what makes you conclude I do not read the info which is given to me?

Just for the record, I am certainly not a kid, and spent a lot of time resolving other issues in Qt without asking questions. I never asked for any code, I just raised a question about an issue I did not understand.

jpn
6th October 2007, 12:17
Looks like it was me who misunderstood your answer. I thought you meant that it was similar but something more trivial that was solved in the linked thread. My apologies for that. No hard feelings.

Raistlin
6th October 2007, 12:23
Indeed a misunderstanding, I just meant people ask the same question twice in this forum sometimes, although the issue is easier than this problem.

No hard feelings back :).