title: Java client socket using writeBytes
body: <p>I'm reading a string from a buffer and writing it to a server. The problem I'm having is that the string never gets received by the server when I leave the socket open and write in a loop. 
  When I use this:</p>
  
  <pre><code>    try {       
          Socket send = new Socket("localhost", 1490);
          DataOutputStream out = new DataOutputStream(send.getOutputStream());
          String message = null;
          while ((message = buffer.get()) != null){
              out.writeBytes(message);
          }
          out.close();
          send.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>the server doesn't receive the string, but when I do this it works properly:</p>
  
  <pre><code>    try {       
  
          String message = null;
          while ((message = buffer.get()) != null){
              Socket send = new Socket("localhost", 1490);
              DataOutputStream out = new DataOutputStream(send.getOutputStream());
                      out.writeBytes(message);
              out.close();
              send.close();
          }
  
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Obviously I don't want to keep opening and closing the socket, though. What is the problem?</p>