Hi,

I have a server and a client. The request client a information to the server. The first message received is answered by the server. But the second only is received after to disconnect and to connect again.


It follow the source code.


The client send request
Qt Code:
  1. void ClientStub::sendRequest(const Message msg)
  2. {
  3. QByteArray block;
  4. QDataStream out(&block, QIODevice::WriteOnly);
  5. out.setVersion(QDataStream::Qt_4_6);
  6.  
  7. out << (quint16)0;
  8. out << msg;
  9.  
  10. out.device()->seek(0);
  11. out << (quint16)(block.size() - sizeof(quint16));
  12.  
  13. tcpSocket->write(block);
  14. tcpSocket->flush();
  15. sended++;
  16. }
To copy to clipboard, switch view to plain text mode 


The server receive and create a ClientSocket object to treat the connection
Qt Code:
  1. void Server::incomingConnection(int socketId)
  2. {
  3. ClientSocket *socket = new ClientSocket();
  4. socket->setSocketDescriptor(socketId);
  5. }
To copy to clipboard, switch view to plain text mode 


ClientSockt code, it is exchange data with the client
Qt Code:
  1. static int countClientSocket = 0;
  2.  
  3. ClientSocket::ClientSocket(QObject *parent)
  4. : QTcpSocket(parent)
  5. {
  6. connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
  7. // connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
  8.  
  9. countClientSocket++;
  10.  
  11. //#ifdef DEBUG_MACHINE
  12. printf("ctor countClientSocket: %i \n", countClientSocket);
  13. std::cout.flush();
  14. //#endif
  15.  
  16. nextBlockSize = 0;
  17.  
  18. }
  19.  
  20. ClientSocket::~ClientSocket()
  21. {
  22. // abort();
  23. countClientSocket--;
  24.  
  25. #ifdef DEBUG_MACHINE
  26. printf("dtor countClientSocket: %i \n", countClientSocket);
  27. std::cout.flush();
  28. #endif
  29.  
  30. }
  31.  
  32. bool ClientSocket::setSocketDescriptor(int socketDescriptor)
  33. {
  34. QTcpSocket::setSocketDescriptor(socketDescriptor);
  35. return waitForReadyRead(5);
  36. }
  37.  
  38. void ClientSocket::receive()
  39. {
  40. QDataStream in(this);
  41. in.setVersion(QDataStream::Qt_4_6);
  42.  
  43. if (nextBlockSize == 0)
  44. {
  45. if (bytesAvailable() < sizeof(quint16))
  46. return;
  47.  
  48. in >> nextBlockSize;
  49. }
  50.  
  51. if (bytesAvailable() < nextBlockSize)
  52. return;
  53.  
  54. Message msg;
  55. in >> msg;
  56.  
  57. invoke(msg);
  58. }
  59.  
  60. void ClientSocket::invoke(Message &msg)
  61. {
  62. QList<QVariant> msgContents = msg.getContents();
  63. WordCounter task = msgContents.first().value<WordCounter>();
  64.  
  65. msg.setTypes(Message::Info);
  66. msg.setContents(QList<QVariant>() << QString("received request"));
  67.  
  68. response(msg);
  69. printf("%i - received request\n", msg.getId());
  70.  
  71. QByteArray normalizedSignature = QMetaObject::normalizedSignature("execute (QList<QVariant> &)");
  72.  
  73. QVariant retVal;
  74. const QMetaObject *metaObject = 0;
  75. metaObject = task.metaObject();
  76. int methodIndex = -1;
  77. methodIndex = metaObject->indexOfMethod(normalizedSignature);
  78. QMetaMethod method = metaObject->method(methodIndex);
  79.  
  80. printf("Processing request of: %i \n", task.getKey());
  81.  
  82. QElapsedTimer timer;
  83. timer.start();
  84. method.invoke(
  85. &task,
  86. Qt::DirectConnection, // Qt::AutoConnection,
  87. Q_RETURN_ARG(QVariant, retVal),
  88. Q_ARG(QList<QVariant>, msgContents)
  89. );
  90. int t = timer.elapsed();
  91.  
  92. msg.setTypes(Message::Result);
  93. msg.setContents(QList<QVariant>() << retVal
  94. << QString("Time elapsed: %1 miliseconds").arg(t)); // << "done!");
  95. printf("id: %i, Result: %i \n--------&gt;>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t);
  96.  
  97. response(msg);
  98.  
  99. // waitForDisconnected();
  100. // close();
  101. }
  102.  
  103. void ClientSocket::response(Message msg)
  104. {
  105. QByteArray block;
  106. QDataStream out(&block, QIODevice::WriteOnly);
  107. out.setVersion(QDataStream::Qt_4_6);
  108.  
  109. out << quint16(0);
  110. out << msg;
  111.  
  112. out.device()->seek(0);
  113. out << quint16(block.size() - sizeof(quint16));
  114.  
  115. write(block);
  116. flush();
  117. }
To copy to clipboard, switch view to plain text mode 


Thanks!!