Problem on the server in my application client-server
-
Hi,
I have a class called ClientSocket which inherits of QTcpSocket.
The first message, the server received and responded for the client;
After, the request client a new information but the server don't receive.If i close the connection, after the first message its work, but i don't do this.
It follow the source code.
The client send request
@
void ClientStub::sendRequest(const Message msg)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);out << (quint16)0; out << msg; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); tcpSocket->write(block); tcpSocket->flush(); sended++;
}
@The server receive and create a ClientSocket object to treat the connection
@
void Server::incomingConnection(int socketId)
{
ClientSocket *socket = new ClientSocket();
socket->setSocketDescriptor(socketId);
}
@ClientSockt code, it is exchange data with the client
@
static int countClientSocket = 0;ClientSocket::ClientSocket(QObject *parent)
: QTcpSocket(parent)
{
connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
// connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));countClientSocket++; //#ifdef DEBUG_MACHINE printf("ctor countClientSocket: %i \n", countClientSocket); std::cout.flush(); //#endif nextBlockSize = 0;
}
ClientSocket::~ClientSocket()
{
// abort();
countClientSocket--;#ifdef DEBUG_MACHINE
printf("dtor countClientSocket: %i \n", countClientSocket);
std::cout.flush();
#endif}
bool ClientSocket::setSocketDescriptor(int socketDescriptor)
{
QTcpSocket::setSocketDescriptor(socketDescriptor);
return waitForReadyRead(5);
}void ClientSocket::receive()
{
QDataStream in(this);
in.setVersion(QDataStream::Qt_4_6);if (nextBlockSize == 0) { if (bytesAvailable() < sizeof(quint16)) return; in >> nextBlockSize; } if (bytesAvailable() < nextBlockSize) return; Message msg; in >> msg; invoke(msg);
}
void ClientSocket::invoke(Message &msg)
{
QList<QVariant> msgContents = msg.getContents();
WordCounter task = msgContents.first().value<WordCounter>();msg.setTypes(Message::Info); msg.setContents(QList<QVariant>() << QString("received request")); response(msg); printf("%i - received request\n", msg.getId()); QByteArray normalizedSignature = QMetaObject::normalizedSignature("execute (QList<QVariant> &)"); QVariant retVal; const QMetaObject *metaObject = 0; metaObject = task.metaObject(); int methodIndex = -1; methodIndex = metaObject->indexOfMethod(normalizedSignature); QMetaMethod method = metaObject->method(methodIndex); printf("Processing request of: %i \n", task.getKey()); QElapsedTimer timer; timer.start(); method.invoke( &task, Qt::DirectConnection, // Qt::AutoConnection, Q_RETURN_ARG(QVariant, retVal), Q_ARG(QList<QVariant>, msgContents) ); int t = timer.elapsed(); msg.setTypes(Message::Result); msg.setContents(QList<QVariant>() << retVal << QString("Time elapsed: %1 miliseconds").arg(t)); // << "done!"); printf("id: %i, Result: %i \n-------->>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t); response(msg); // waitForDisconnected(); // close();
}
void ClientSocket::response(Message msg)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);out << quint16(0); out << msg; out.device()->seek(0); out << quint16(block.size() - sizeof(quint16)); write(block); flush();
}
@
Thanks!!
-
reset nextBlockSize to 0 once you're done with the workload.
Otherwise you will never get the next block size in line 43.
Set some breakpoints and run the app in a debugger, you'll see that you enter the if block only once.