Hi there,
I'm having a problem with reading an incomming connection on my QTcpServer.
i create a QtcpServer object and connect the signal newconnection() to a custom made slot:
connect(serv, SIGNAL(newConnection()), this, SLOT(newsession()));
serv = new QTcpServer();
connect(serv, SIGNAL(newConnection()), this, SLOT(newsession()));
serv->listen(QHostAddress::Any, port);
To copy to clipboard, switch view to plain text mode
then from the slot for each pending connection i create a thread to handle the incomming data:
void clienthandler::newsession()
{
incomingslientsession *client;
while(serv->hasPendingConnections()){
client = new incomingslientsession(&db, serv->nextPendingConnection()->socketDescriptor(), &mutex);
connect(t, SIGNAL(started()), client, SLOT(dowork()));
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); // Delete thread after session is handeled
client->moveToThread(t);
t->start();
}
}
void clienthandler::newsession()
{
QThread *t;
incomingslientsession *client;
while(serv->hasPendingConnections()){
t = new QThread();
client = new incomingslientsession(&db, serv->nextPendingConnection()->socketDescriptor(), &mutex);
connect(t, SIGNAL(started()), client, SLOT(dowork()));
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); // Delete thread after session is handeled
client->moveToThread(t);
t->start();
}
}
To copy to clipboard, switch view to plain text mode
In the thread i create a new Qtcpsocket() and assign the given socketdescriptor. After that i try to read 20 bytes from the incomming stream (sending node sends 1000+ bytes)
sock.setSocketDescriptor(socketdescriptor);
// Test code to read some bytes
int bytesRead = 0;
while (bytesRead < 20) // First 20 bytes
bytesRead = sock.read(buff.data(), 20-bytesRead);
qDebug() << buff;
QTcpSocket sock;
sock.setSocketDescriptor(socketdescriptor);
// Test code to read some bytes
int bytesRead = 0;
QByteArray buff;
while (bytesRead < 20) // First 20 bytes
bytesRead = sock.read(buff.data(), 20-bytesRead);
qDebug() << buff;
To copy to clipboard, switch view to plain text mode
This is where the problem is, the socket never receives any bytes so this code gets stuck in an endless loop! i can see the data coming in when looking with wireshark and the socketdescriptor is also the right one.
Can anyone see the problem here?
Thanks,
Sisco
Bookmarks