title: Sending an xml message in parts through a TCP socket using Qt
body: <p>We are writing a project where there is a client who generates xml requests, sends them to a server, who parses the request and returns the requested information in an xml string.</p>
  
  <p>The application works fine when the xml replies are small, but when they exceed about 2500 characters they sometimes get cut off on the client end. I say sometimes because when the client and server run on the same machine and communicate through the home address 127.0.0.1 the replies are parsed just fine. However, when the client and server are on different machines and communicate over lan, this is when the client cuts the message to around 2500 characters.</p>
  
  <p>The communication is done by tcp sockets. We are using Qt, the client has a qTCPsocket, and the server a qTCPserver and a pointer to a qtcpsocket.</p>
  
  <p>We think a possible solution to our issue is sending the xml in pieces, either separated by character count or by tag. While it is easy for us to break the message into parts, sending the parts and having the client or server read and compile the parts into one xml request is causing us trouble.</p>
  
  <p>For the sake of example, we wanted to test having the client send a request in multiple parts.</p>
  
  <p>Here is our client function call to send a request. xmlReq is generated else where and passed in. As an example of breaking the message into parts we strip away the closing tag from the xml request, and then send it as another piece later.</p>
  
  <pre><code>QString ClientConnection::sendRequest(QString xmlReq)
  {
  
      this-&gt;xmlRequest = xmlReq;
  
      QHostAddress addr(address);
  
      QList&lt;QString&gt; messagePieces;
      xmlRequest.remove("&lt;/message&gt;");
  
      messagePieces.append(xmlRequest);
      messagePieces.append("&lt;/message&gt;");
  
  
      client.connectToHost(addr,6789);
  
      if(client.waitForConnected(30000))
      {
          for(int i = 0; i &lt; messagePieces.length();i++)
          {  
              client.write(messagePieces[i].toAscii(),messagePieces[i].length()+1);
              qDebug() &lt;&lt; "Wrote: " &lt;&lt; messagePieces[i];
          }
      }
  
  
      char message[30000] = {0};
  
      xmlReply = "";
  
      if(client.waitForReadyRead(30000)){
  
  
          client.read(message,client.bytesAvailable());
  
  
      }else{
          xmlReply = "Server Timeout";
      }
  
      client.close();
      xmlReply = (QString) message;
  
      return xmlReply;
  
  }
  </code></pre>
  
  <p>Next is our server code. It is written in such away that it is supposed to read messages from the client until it sees the xml closing message tag, and then process the data and send the reply back to the client.</p>
  
  <p>This is the code that starts the server.</p>
  
  <pre><code>//Start the server, pass it the handler so it can perform queries
      connect(&amp;server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
      server.listen(QHostAddress::Any, 6789);
  </code></pre>
  
  <p>When it gets a new connection it calls the acceptConnection slot which looks like this</p>
  
  <pre><code>    void CETServer::acceptConnection()
  {
      client = server.nextPendingConnection();
      connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
  }
  </code></pre>
  
  <p>The startRead looks like this:</p>
  
  <pre><code>void CETServer::startRead()
  {
      char buffer[1024*30] = {0};
  
      client-&gt;read(buffer, client-&gt;bytesAvailable());
  
      QString readIn;
  
      readIn = (QString) buffer;
  
      ui-&gt;statusText-&gt;appendPlainText("Received: " + readIn);
  
      //New messages in will be opened with the xml version tag
      //if we receive said tag we need to clear our query
      if (readIn.contains("&lt;?xml version =\"1.0\"?&gt;",Qt::CaseSensitive))
      {
          xmlQuery = "";
      }
  
      //add the line received to the query string
      xmlQuery += readIn;
  
      //if we have the clsoe message tag in our query it is tiem to do stuf with the query
      if(xmlQuery.contains("&lt;/message&gt;"))
      {
          //do stuff with query
  
          ui-&gt;statusText-&gt;appendPlainText("Query received:" + xmlQuery);
  
          QString reply = this-&gt;sqLite-&gt;queryDatabase(xmlQuery);
          xmlQuery = "";
  
          this-&gt;commandStatus(reply);
  
          if(client-&gt;isWritable()){
              //write to client
              client-&gt;write(reply.toAscii(),reply.length()+1);
              ui-&gt;statusText-&gt;appendPlainText("Sent to client: " + reply);
              client-&gt;close();
  
          }
      }}
  </code></pre>
  
  <p>In my mind, the start read is coded in such a way that anytime the client writes a message, the server reads it and attaches it to the xmlRequest the server stores. If the message contains , the xml closing tag, then it processes the request.</p>
  
  <p>What happens though, is if the client does successive writes, the server does not read them all, only the first, and never receives the xml  closing tag, and therefore processes no requests.</p>
  
  <p>The question I need answered is why doesn't the server respond to the clients multiple writes? How should I go about making it such that I can send an xml string, broken down in pieces, and have the server read all the pieces and turn it into one string again?</p>