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:
server_response.png
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_);
}
void Server::incomingConnection(const int socketDescriptor)
{
#if (DEBUGGING)
qDebug()<<"Server::incommingConnection()";
#endif
client=new NetworkObjects::Client(socketDescriptor,parent_);
}
To copy to clipboard, switch view to plain text mode
Client
::Client(const int socketDescriptor,
QObject *parent
) : parent_(parent)
{
setSocketDescriptor(socketDescriptor);
//initialize socket
//connect signals/slots of socket
QObject::connect(tcpSocket,
SIGNAL(connected
()),
this,
SLOT(connected
()));
QObject::connect(tcpSocket,
SIGNAL(disconnected
()),
this,
SLOT(disconnected
()));
QObject::connect(tcpSocket,
SIGNAL(readyRead
()),
this,
SLOT(readyRead
()));
this->tcpSocket->setSocketDescriptor(this->socketDescriptor());
if (this->tcpSocket->waitForReadyRead(kWaitConnectingMillisecond))
qDebug()<<"New client connected";
else
qDebug()<<"Error while connecting client: "+tcpSocket->errorString();
}
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()),this,SLOT(connected()));
QObject::connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
QObject::connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
this->tcpSocket->setSocketDescriptor(this->socketDescriptor());
if (this->tcpSocket->waitForReadyRead(kWaitConnectingMillisecond))
qDebug()<<"New client connected";
else
qDebug()<<"Error while connecting client: "+tcpSocket->errorString();
}
To copy to clipboard, switch view to plain text mode
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
see the app instance 1 in above image sends request successfully and app instance 2 receives it fine and creates response
{
#if (DEBUGGING)
qDebug
()<<
"RequestManager::send() " + QString(*request
) + " SENDING TO: "+address_
+":"+QString::number(port_
);
#endif
sendTcpSocket->abort();
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
can we please try to help me out?
thanks
Bookmarks