I am still very new to C++/Qt programming. I was hoping that someone could shed some light on something for me:

Qt Code:
  1. void Thread::run()
  2. {
  3. //initalize socket, start connection and detect data available
  4. socket = new QTcpSocket;
  5.  
  6. if (socket->setSocketDescriptor(sock))
  7. {
  8. QString welcomeMsg = QString("Welcome to the middleMan\n\n");
  9. QByteArray block;
  10. QDataStream out(&block, QIODevice::WriteOnly);
  11. out.setVersion(QDataStream::Qt_4_0);
  12. out << (quint16)0;
  13. out << welcomeMsg;
  14. out.device()->seek(0);
  15. out << (quint16)(block.size() - sizeof(quint16));
  16.  
  17. socket->write(block);
  18. }
  19.  
  20. while (socket->state() == QAbstractSocket::ConnectedState)
  21. {
  22.  
  23. if (socket->bytesAvailable() < 1)
  24. {
  25. qDebug("Waiting for data...");
  26.  
  27. if (!socket->waitForReadyRead())
  28. break;
  29. }
  30.  
  31. // while (socket->bytesAvailable() > 0)
  32. // {
  33. if (socket->bytesAvailable() > 0)
  34. {
  35. qDebug("Data available");
  36.  
  37. QByteArray incomingBlock = socket->readAll();
  38.  
  39. if (incomingBlock.contains("xml") && !incomingBlock.isEmpty())
  40. {
  41. dataHandler = new xmlDataHandler;
  42.  
  43. dataHandler->doParse( &incomingBlock );
  44. }
  45.  
  46. /*
  47.   char c;
  48.  
  49.   socket->getChar(&c);
  50.  
  51.   printf("%c", c);
  52.   */
  53. }
  54. // }
  55. }
  56.  
  57. qDebug("Connection closed");
  58. }
To copy to clipboard, switch view to plain text mode 

To explain a little bit. This is the run code for a thread that is created and started each time a new connection is requested. I am trying to stream short bits of dynamically created xml like data to the server from a client. I basically want to know when the end of a stream has been reached. How do you figure out the data that is coming across the socket. I assume the beginning of the stream is the header information. Then the data is passed. How do I separate those and only save the data and close the connection after the data is finished streaming? Thanks in advance. Even pointing to a good sockets tutorial would suffice.