PDA

View Full Version : double QSignalMapper with QTcpServer/QTcpSocket



radeberger
5th May 2010, 07:42
Hi guys,

I have one QTcpServer that must deal with several QTcpSocket clients. For this purpose I wanted to use 2 QSignalMapper for the newConnection() and readyRead signals as follows:



// this runs in a separate thread
void myServerThread::run(){

for(count=0;count<numConn;count++){
signalMapperAcceptConnection->setMapping( tcpServer[count], count);
signalMapperReadData->setMapping( tcpClient[count], count);
connect(tcpServer[count], SIGNAL(newConnection()), signalMapperAcceptConnection, SLOT(map()));
}
connect( signalMapperAcceptConnection, SIGNAL(mapped(int)), this, SLOT(acceptConnection(int)),
Qt::DirectConnection);
exec();
}

void myServerThread::acceptConnection(int objectID){
static unsigned short signalCounter = 0;
tcpClient[objectID] = tcpServer[objectID]->nextPendingConnection();
connect( tcpClient[objectID], SIGNAL(readyRead()), signalMapperReadData, SLOT(map()));
signalCounter++;
if(signalCounter == numConn)
connect( signalMapperReadData, SIGNAL(mapped(int)), this, SLOT(startRead(int)), Qt::DirectConnection);
}



The first signal mapper works fine, the signal newConnection() is mapped into the acceptConnection slot with the correct ID but the problem is with the second signalmapper. The signal readyRead() doesn't come, althought a client is sending data.

Do you know what I'm doing wrong?

Thanks in advance!

squidge
5th May 2010, 08:01
I'm a little confused by your last connect statement. You only connect up the mapping when all connected slots are setup?

radeberger
5th May 2010, 09:26
well, I tried both, connect one per client comes, and only one time, when all clients has been connected. In my application the connection of the clients happens at the beggining of the programm and it's controlated, I mean thats not a chat application or something so... My application "trigers" the clients at the beggining, so that, they try to connect...

wysota
5th May 2010, 09:56
If the code of run() you posted is your actual code, then it won't work. The sockets (and the signal mapper) need to be created in the thread you wish them to be handled by, otherwise your thread will do nothing apart making the connections.

radeberger
5th May 2010, 11:39
thanks wysota,

I created the sockets and the signal mappers in the run and I connected the signal of the sockets with the mappers in the run as well, but I still have the problem that I don't get the readRead() signal,

shall I post the code?

wysota
5th May 2010, 18:46
Please post a minimal compilable example reproducing the problem.