title: QTcpServer with android client unable to print or use data received from client
body: <p>I am developing Client-Server application in C++ using Qt framework, but the clients can be android phones and computers(Qt client app)
  Now i'm having troubles to handle Reception of data on the server side; the server is not receiving data properly.</p>
  
  <p>First, I got things working nicely between the server(Qt app) and the client(Qt app)  using these methods for sending and receiving:
  The size of the message is kept at the beginning of the packet to help check whether the whole message is received or not.</p>
  
  <p>This is the method to send message to the clients</p>
  
  <pre><code>void Server::send(const QString &amp;message)
  {
      QByteArray paquet;
      QDataStream out(&amp;paquet, QIODevice::WriteOnly);
  
      out &lt;&lt; (quint16) 0; // just put 0 at the head of the paquet to reserve place to put the size of the message
      out &lt;&lt; message; // adding the message
      out.device()-&gt;seek(0); // coming back to the head of the paquet
      out &lt;&lt; (quint16) (paquet.size() - sizeof(quint16)); // replace the 0 value by the real size
  
  
      clientSocket-&gt;write(paquet); //sending...
  
  }
  </code></pre>
  
  <p>This slot is called every time a single paquet is received.</p>
  
  <pre><code>void Server::dataReceived()
  {
      forever 
      {
          // 1 : a packet has arrived from any client
  
          // getting the socket of that client (recherche du QTcpSocket du client)
          QTcpSocket *socket = qobject_cast&lt;QTcpSocket *&gt;(sender());
          if (socket == 0) 
              return; 
  
          QDataStream in(socket);
  
          if (dataSize == 0) // if we don't know the size of data we are suppose to receive...
          {
              if (socket-&gt;bytesAvailable() &lt; (int)sizeof(quint16)) // we haven't yet receive the size of the data completly then return...
                   return;
  
              in &gt;&gt; dataSize; // now we know the amount of data we should get
          }
  
          if (socket-&gt;bytesAvailable() &lt; dataSize) 
              return;
  
          // Here we are sure we got the whole data then we can startreadind
          QString message;
          in &gt;&gt; message;
  
          //Processing....
  
          dataSize = 0; // re-initialize for the coming data
      }
  }
  </code></pre>
  
  <p>This is working well when the server is talking  with the Qt app Client, because the same methods are used there, and the size of quint16 will remain the same hover it doesn't work with android client, then i tried another way in which i wanted to ignore the size of the message sent, but format the message in a way such that i can know where it starts and where it ends, then with some controls i can get it however i'm stuck here, cause the data read doesn't contain anything when printed, but his size has a value(which even vary according to the amount of text the client send)!</p>
  
  <pre><code>void Server::dataReceived() // a packet is received!
  {
  
      QTcpSocket *socket = qobject_cast&lt;QTcpSocket *&gt;(sender());
      if (socket == 0)
          return;
  
  
      QByteArray data= socket-&gt;readAll(); //reading all data available
      QString message(data)
  
      qDebug() &lt;&lt; data;        // this prints nothing!
      qDebug() &lt;&lt; data.size();//  But this prints a non null number, wich means we got something, and that number varies according to the amount of text sent!
      qDebug() &lt;&lt; message;    // this also prints notghing!
  
  } 
  </code></pre>
  
  <p>PS: it's not working even for the Qt app Client.</p>
  
  <p>Can you help me find out what's wrong, i'm a bit confused how the tcp protocol is handling the data, and if you could and also advise me a good way for doing this.</p>
  
  <p>here is the android class I made for the purpose</p>
  
  <pre><code>class QTcpSocket implements Runnable {
  
      private String ip="";
      private int port;
      private Socket socket;
      private PrintWriter printWriter;
  
      private DataOutputStream dataOutputStream;
      private DataInputStream dataInputStream;
  
      public QTcpSocket(String ip, int port) {
          this.ip = ip;
          this.port = port;
      }
  
        public void setIp(String ip) {
            this.ip = ip;
        }
  
        public String getIp() {
            return this.ip;
        }
  
      public void setPort(int port) {
          this.port = port;
      }
  
      public void run() {
          try {
              socket = new Socket(this.ip, this.port);
              dataOutputStream = new DataOutputStream( socket.getOutputStream() );
              dataInputStream = new DataInputStream(socket.getInputStream());
              String response = dataInputStream.readUTF();
              dataOutputStream.writeUTF("Hello server!");
  
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  
      public void sendMessage(String message) {
          try {
  
              dataOutputStream.writeUTF(message);
          }catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void disconnect() {
          try {
  
              printWriter.flush();
              printWriter.close();
              socket.close();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public boolean isClosed() {
          return socket.isClosed();
      }
  }
  </code></pre>