PDA

View Full Version : QTcpServer with threaded clients problem



Witek
16th September 2011, 08:56
Hi.

I've writing TcpServer app in Qt and client in avr32 processor, but i got problem with server. My server should be multi-threaded, each of client on other thread. My problem is:

When I've connected newConnection() signal with this slot:


void CTcpServer::incomingConnection(int socketDescriptor)
{
//IF there are still empty slots
if(numOfClients < MAX_CLIENTS)
{
//Check which one is empty
for(int i=0; i<MAX_CLIENTS; i++)
{
//Empty slot found
if( 0 == clients[i])
{
//got socket
clients[i] = new CTargetThread(socketDescriptor,this,socket);
connect(clients[i], SIGNAL(finished()), clients[i], SLOT(deleteLater()));
clients[i]->start();
//Client counter indicate how much clients are connected to server
numOfClients++;
break;
}
}
}
}

it simply check is a client slot aviable and runs thread for connected client.
and there is CTargetThreads run method:



void CTargetThread::run()
{
socket=new QTcpSocket();
if (!socket->setSocketDescriptor(iSocketDescriptor)) {
//emit error(tcpSocket.error());
qDebug("setSocketDescriptor failed");
return;
}
qDebug("Socket granted");
while(1)
{
socket->write("Sample message");
msleep(50);
}
socket->disconnectFromHost();
socket->waitForDisconnected();
}

And problem is:
my packet with "sample message" doesn't seems to be send, I use WireShark to look up for packets, and it does not shows that packet.

Thread is created normally, setSocketDescriptor returns false, so it success, and when i write something, it just not working.

When I write something on client(avr32) side, it can't be readed in thread, but i see in WireShark, that this packet have been sent.

please, help me :)

Jonny174
19th September 2011, 06:49
How you reading? Try: socket->write("Sample message\n");

marcvanriet
21st September 2011, 23:10
Thread is created normally, setSocketDescriptor returns false, so it success, and when i write something, it just not working.


From the Qt doc :

bool QAbstractSocket::setSocketDescriptor ( int socketDescriptor, SocketState socketState = ConnectedState, OpenMode openMode = ReadWrite )

Initializes QAbstractSocket with the native socket descriptor socketDescriptor. Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false.

Regards,
Marc