PDA

View Full Version : Network Threaded server problem



^NyAw^
22nd May 2008, 17:42
Hi,

I have a class "CServer" that inherits from "QTcpServer". When it recives an incoming connection it creates a new QThread that recives the socketDescriptor.



void CServer::incomingConnection(int iSocketDescriptor)
{
CThread* pCThread = new CThread(this,iSocketDescriptor);
bool bC = connect(pCThread,SIGNAL(finished()),pCThread,SLOT( deleteLater()));
pCThread->start();
}


The Thread creates a Socket into "run()" method:


void CThread::run()
{
//Create the socket without parent
m_pqSocket = new QTcpSocketThread();
if (!m_pqSocket->setSocketDescriptor(m_iSocketDescriptor))
{
emit error(m_pqSocket->error());
return;
}
//Enter the eventLoop
exec();

//The client disconnects
m_pqSocket->waitForDisconnected();
delete (m_pqSocket);
}


The clients are sending data to the client, so the server has a thread that is reciving the data from the client.

One problem is that the, trying to connect 2 clients, the server creates 2 threads, but only the first thread that is responding tho the first client is responding. When the first client close the connection that makes the server thread to exit, then the second thread recives communication.
Also, the second thread don't recives all the data.

Am I doing something wrong on the code above?

Thanks,

^NyAw^
22nd May 2008, 20:22
Hi,

Every Thread has a QTcpSocket, so why the socket of the first connection recives data through the socket and the second one don't recive any data?

The Thread is doing a job that maybe consumes a lot of memory. Could this be a problem when the second connection tries to create the second thread?


Thanks,

mcosta
22nd May 2008, 22:06
A simple question:
Are you sure that the run() funciotn is called for second Thread?

try this:



void CThread::run()
{
qDebug("I'm Here");

//Create the socket without parent
m_pqSocket = new QTcpSocketThread();
....

}


Another question:
where do you set connections with m_pqSocket signals?

^NyAw^
23rd May 2008, 09:08
Hi,

It's not a memory problem. I have changed the process code to "msleep" and the second Thread was locked.


I've changed the way that I was doing it work and now it works well.