PDA

View Full Version : QTcpSocket->readAll() doesn't have anything to read!



naturalpsychic
9th April 2012, 04:05
I have server and client in the same client/server architecture application.

First send
Server::incommingConnection()
Buffering
"Task::handleTask strBuffer= taskToHandle---received from localhost:1591"

"RequestManager::send() response SENDING TO: localhost:1591"
RequestManager::send() CONNECTING...
RequestManager::send() CONNECTED
RequestManager::send() SENT!
New client connected


is debug output


now my problem is that it stops at New client connected and doesn't goto readyRead() infact even when I explicitely call readyRead() [my slot connected to QTcpSocket's readyRead signal] readAll() has nothing in it

following diagram is better way of describing of whats happening:

7568

Following are some bits of my code:


void Server::incomingConnection(const int socketDescriptor)
{
#if (DEBUGGING)
qDebug()<<"Server::incommingConnection()";
#endif
client=new NetworkObjects::Client(socketDescriptor,parent_);

}

Client::Client(const int socketDescriptor,QObject *parent) :
QObject(parent),
parent_(parent)
{
setSocketDescriptor(socketDescriptor);
//initialize socket
this->tcpSocket = new QTcpSocket(this);
tcpSocket->open(QTcpSocket::ReadWrite);

//connect signals/slots of socket
QObject::connect(tcpSocket,SIGNAL(connected()),thi s,SLOT(connected()));
QObject::connect(tcpSocket,SIGNAL(disconnected()), this,SLOT(disconnected()));
QObject::connect(tcpSocket,SIGNAL(readyRead()),thi s,SLOT(readyRead()));


this->tcpSocket->setSocketDescriptor(this->socketDescriptor());
if (this->tcpSocket->waitForReadyRead(kWaitConnectingMillisecond))
qDebug()<<"New client connected";
else
qDebug()<<"Error while connecting client: "+tcpSocket->errorString();
}

printing this tcpSocket->errorString() gives error "The remote host closed the connection".
I even tried to comment out all closing statements that closes socket (for debugging purpose) but didn't do


void Client::readyRead(void)
{
qDebug()<<"Buffering";
NetworkObjects::Task *task=new NetworkObjects::Task(parent_,tcpSocket->readAll());
QObject::connect(task,SIGNAL(taskCompleted()),task ,SLOT(deleteLater()));
task->handleTask();

}

see the app instance 1 in above image sends request successfully and app instance 2 receives it fine and creates response


void RequestManager::send(QByteArray *request)
{
#if (DEBUGGING)
qDebug()<<"RequestManager::send() " + QString(*request) + " SENDING TO: "+address_+":"+QString::number(port_);
#endif
sendTcpSocket->abort();
sendTcpSocket->open(QTcpSocket::ReadWrite);

sendTcpSocket->connectToHost(address_,port_);
#if (DEBUGGING)
qDebug()<<"RequestManager::send() CONNECTING...";
#endif

if (sendTcpSocket->waitForConnected(3000))
{
#if (DEBUGGING)
qDebug()<<"RequestManager::send() CONNECTED";
#endif
}


if (sendTcpSocket->isOpen()==false){
emit socketCannotOpen();
}
else if (sendTcpSocket->isWritable())
{


sendTcpSocket->write(*request);

#if (DEBUGGING)
qDebug()<<"RequestManager::send() SENT!";
#endif
}

// sendTcpSocket->close();

}


can we please try to help me out?

thanks

naturalpsychic
9th April 2012, 08:53
no one?

can someone please reply (only if you know the answer)?

mentalmushroom
9th April 2012, 10:02
Maybe you should call waitForConnected before calling waitForReadyRead. Also consider non-blocking approach.

Spitfire
9th April 2012, 16:20
Attached is simple client-server demo app, see if it works for you and try to resolve your issue based on it.