title: Transmiting/receiving compressed data with sockets: how to properly receive the data sent from the client
body: <p>I have developed a client-server chat using the Sockets and it works great, but when I try to transmit data with Deflate compression it doesn't work: the output is "empty" (actually it's not empty, but I'll explain below).</p>
  
  <p>The compression/decompression part is 100% working (I have already tested it), so the problem must be elsewhere in the transmission/receiving part.</p>
  
  <p>I send the message from the client to the server using these methods:</p>
  
  <pre><code>// streamOut is an instance of DataOutputStream
  // message is a String
  
  if (zip) { // zip is a boolean variable: true means that compression is active
      streamOut.write(Zip.compress(message)); // Zip.compress(String) returns a byte[] array of the compressed "message"
  } else {
      // if compression isn't active, the client sends the not compressed message to the server (and this works great)
      streamOut.writeUTF(message);
  }
  streamOut.flush();
  </code></pre>
  
  <p>And I receive the message from the client to the server using these other methods:</p>
  
  <pre><code>// streamIn is an instace of DataInputStream
  
  if (server.zip) { // same as before: true = compression is active
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] buf = new byte[512];
      int n;
  
      while ((n = streamIn.read(buf)) &gt; 0) {
          bos.write(buf, 0, n);
      }
  
      byte[] output = bos.toByteArray();
      System.out.println("output: " + Zip.decompress(output)); // Zip.decompress(byte[]) returns a String of decompressed byte[] array received
  } else {
      System.out.println("output: " + streamIn.readUTF()); // this works great
  }
  </code></pre>
  
  <p>Debugging a little bit my program, I've discovered that the while loop never ends, so:</p>
  
  <pre><code>byte[] output = bos.toByteArray();
  System.out.println("output: " + Zip.decompress(output));
  </code></pre>
  
  <p>is never called.</p>
  
  <p>If I put those 2 lines of code in the while loop (after <em>bos.write()</em>), then all works fine (it prints the message sent from the client)! But I don't think that's the solution, because the byte[] array received may vary in size. Because of this I assumed that the problem is in the receiving part (the client is actually able to send data).</p>
  
  <p>So my problem became the while loop in the receiving part. I tried with:</p>
  
  <pre><code>while ((n = streamIn.read(buf)) != -1) {
  </code></pre>
  
  <p>and even with the condition != 0, but it's the same as before: the loop never ends, so the output part is never called.</p>