Hi all,

Please forget my lack of experience and knowledge about Qt.
I did look for answers in google, Qt Doc, and through this forum, but i got quite confused.

I'm currently writing a client-server application.
Communication are handled that way :
- Client connects to server and sends a request
- Server executes the request and sends back an answer
- Once the answer is fully received, client disconnects.

Some request can take some times (2-3sec) and i want the clients (Multi client) to be able to send other request while waiting for the answer.

For the time being, i'm using "Threaded Fortune Server Example" to handle multiple connections.
Qt Code:
  1. class TcpServer : public QTcpServer
  2. {
  3. Q_OBJECT
  4. public:
  5. TcpServer(QObject *parent);
  6. protected:
  7. void incomingConnection(int socket_descriptor);
  8.  
  9. void TcpServer::incomingConnection(int socket_descriptor)
  10. {
  11. TcpServerThread *thread = new TcpServerThread(socket_descriptor, this);
  12. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
  13. thread->start();
  14. }
To copy to clipboard, switch view to plain text mode 
And i'm handling communication in a synchronous approach.
Qt Code:
  1. void TcpServerThread::run()
  2. {
  3. QTcpSocket tcp_socket;
  4.  
  5. if (!tcp_socket.setSocketDescriptor(m_socket_descriptor))
  6. {
  7. emit error(tcp_socket.error(), tcp_socket.errorString());
  8. return;
  9. }
  10.  
  11. /* Wait for request size...................................................*/
  12.  
  13. while (tcp_socket.bytesAvailable() < (int)sizeof(qint32))
  14. {
  15. if (!tcp_socket.waitForReadyRead())
  16. {
  17. emit error(tcp_socket.error(), tcp_socket.errorString());
  18. return;
  19. }
  20. }
  21.  
  22. /* Read request size.......................................................*/
  23.  
  24. qint32 blocksize = 0;
  25. QDataStream in(&tcp_socket);
  26. in.setVersion(QDataStream::Qt_4_0);
  27. in.readRawData((char*) &blocksize, sizeof(qint32));
  28.  
  29. /* Wait until full request written on socket...............................*/
  30.  
  31. while (tcp_socket.bytesAvailable() < blocksize)
  32. {
  33. if (!tcp_socket.waitForReadyRead())
  34. {
  35. emit error(tcp_socket.error(), tcp_socket.errorString());
  36. return;
  37. }
  38. }
  39.  
  40. /* Read request............................................................*/
  41.  
  42. char* request = (char*) malloc(blocksize);
  43. in.readRawData(request, blocksize);
  44.  
  45. /* Execute request and build answer........................................*/
  46.  
  47. mutex.lock();
  48. // etc ...
  49. // return an allocated data_block
  50. mutex.unlock();
  51.  
  52. /* Write answer to client in socket........................................*/
  53.  
  54. QByteArray block;
  55. QDataStream out(&block, QIODevice::WriteOnly);
  56. out.setVersion(QDataStream::Qt_4_0);
  57.  
  58. out.writeRawData(data_block, data_size);
  59.  
  60. tcp_socket.write(block);
  61.  
  62. /* Wait until answer fully read by client..................................*/
  63.  
  64. tcp_socket.waitForDisconnected();
  65.  
  66. // free memory etc.
  67. }
To copy to clipboard, switch view to plain text mode 
Still i'm not sure this code is safe.
I don't fully understand how QThread works.
Is m_socket_descriptor, my only member variable, shared between each instance of TcpServerThread ? Or is it allocated on the thread stack ?

I'm also using a mutex to enable only one thread at a time to "Execute request and build answer". The mutex is a member of a singleton managing the "Execute request and build answer" class/method. Is it the right way to do it ? Should the mutex be a member of TcpServerThread instead ?

On the client side, i want to be able to make several request at the same time since some request can be time-consuming and the client needs to receive continious updates from the server.
I haven't coded this part yet. This is what i want to do :
To achieve this goal, each request will be handled by a thread :
Qt Code:
  1. void Client::newResquest(...)
  2. {
  3. ClientThread *thread = new ClientThread(this, /* various parameters */);
  4. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
  5. thread->start();
  6. }
To copy to clipboard, switch view to plain text mode 
The run() method in ClientThread will handle comminucations just like TcpServerThread in synchronous approach.
And once the answer is fully received, ClientThread will emit a signal to a Client slot, to handle the answer. And then disconnect from server -> ClientThread is terminated.

My question is : if a ClientThread emits a signal to Client while he is already processing that signal (executing the slot connected to the signal), what happens ?
Since receiver and emmiter are in different threads, behavior is Qt::QueuedConnection ? First receiver executes slots, then second receiver waits for first one to finish executing the connected slots and then executes ?

Thank you in advance for your inputs.
I'm quite new to Qt and some answers from an advanced user would make me win some hudge amout of time