PDA

View Full Version : QThread + QTcpServer



Fastman
19th July 2007, 15:25
There is a following code:



class Connection : public QTcpSocket
{
Q_OBJECT
public:
Connection(QObject *parent = 0);
private slots:
void processReadyRead();
};




Connection::Connection(QObject *parent)
: QTcpSocket(parent)
{

QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));

}


void Connection::processReadyRead()
{
char *szData = 0;
if( this->isReadable())
{
qint64 qnSize = this->bytesAvailable();
if( qnSize > 0)
{
szData = new char [qnSize];
qint64 qnReadSize = this->read( szData, qnSize);
szData[qnReadSize]=0;
}
}
Ui_HSMClass *pHSMClass = CProjectManager::GetPrjMng()->m_pHSMClass;
if(pHSMClass != NULL)
{
pHSMClass->addMSG(szData);
}
this->disconnectFromHost();
this->waitForDisconnected();
}




class FortuneThread : public QThread
{
Q_OBJECT
public:
FortuneThread(int socketDescriptor,QObject *parent);
void run();
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
};




FortuneThread::FortuneThread(int socketDescriptor,QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor)
{

}
void FortuneThread::run()
{
Connection *connection = new Connection(this);
connection->setSocketDescriptor(socketDescriptor);

}




class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QObject *parent = 0);
signals:
void newConnection(Connection *connection);
protected:
void incomingConnection(int socketDescriptor);

};




Server::Server(QObject *parent)
: QTcpServer(parent)
{
if (!listen(QHostAddress::Any,1718)) {
return;
}
}

void Server::incomingConnection(int socketDescriptor)
{
FortuneThread *thread = new FortuneThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}




class HSM : public QDialog
{
Q_OBJECT
public:
CProjectManager *m_pPrj;
HSM(QWidget *parent = 0, Qt::WFlags flags = 0);
~HSM();
private:
Ui_HSMClass ui;
Server server;
private slots:
void on_pushButton_clicked();

};



The client connect to a server, but function processReadyRead() does not work ..Why ???
can help me ???

jpn
19th July 2007, 15:32
Could you please change "qtclass" tags to "code" to make it readable?

PS. It's the button labeled "#". Button labeled "Qt" is for referencing to Qt docs.

Fastman
19th July 2007, 15:35
sorry :)
:)

jpn
19th July 2007, 15:42
void FortuneThread::run()
{
Connection *connection = new Connection(this);
connection->setSocketDescriptor(socketDescriptor);

}


This should produce a warning in the debug output. Every QObject has a tread affinity. You cannot create a QObject with a parent "living in" a different thread. FortuneThread object itself lives in the main GUI thread (where it was created), which is different thread than what's being executed in FortuneThread::run(). Also, QThread finishes by the time execution returns from QThread::run() so in your case this happens immediately.

Give it a try with something like this:


void FortuneThread::run()
{
Connection connection;
connection.setSocketDescriptor(socketDescriptor);
connect(&connection, SIGNAL(disconnected()), this, SLOT(quit()));
exec(); // blocks as long as the event loop runs
}


PS. Thanks for reformatting the code ;)

Fastman
19th July 2007, 16:19
Jpn !
thanks, also excuse for my English, I from Minsk:)