title: Reusing the inputstream of a socket
body: <p>i would like to know how to keep the input stream of a socket and reuse it until the application is close.
  What i do for now is creating a thread in the main method.  This thread is supposed to keep running for the all time the application runs.  In this thread i read data from the server using the socket input stream.  But i'm able to read only one time what the server is sending.  After that i think the thread is dead or i cannot read from the input stream.  How can i do to keep the input stream reading what is coming from the server.
  Thanks.</p>
  
  <pre><code>int length = readInt(input);
  
  
      byte[] msg = new byte[length];
      input.read(msg);
  ByteArrayInputStream bs = new ByteArrayInputStream(msg);
              DataInputStream in = new DataInputStream(bs);
              int cmd = readInt(in);
  switch(cmd) {
  case 1: Msg msg = readMsg(cmd, msg);
  }
  </code></pre>
  
  <p>I put here everything, but in my code things happen in different methods.</p>
  
  <p>The readInt method:</p>
  
  <pre><code>public static int readInt(InputStream in) throws IOException {
      int byte1 = in.read();
      int byte2 = in.read();
      int byte3 = in.read();
      int byte4 = in.read();
      if (byte4 == -1) {
          throw new EOFException();
      }
      return (byte4 &lt;&lt; 24)
              + ((byte3 &lt;&lt; 24) &gt;&gt;&gt; 8)
              + ((byte2 &lt;&lt; 24) &gt;&gt;&gt; 16)
              + ((byte1 &lt;&lt; 24) &gt;&gt;&gt; 24);
  }
  </code></pre>
  
  <p>Used for little-endian conversion.</p>