title: TCP/IP Client : best way to read multiple inputstreams from server
body: <p>I'm creating a java Client program that sends a command to server and server sends back an acknowledgement and a response string.</p>
  <blockquote>
  <p>The response is sent back in this manner</p>
  <p>client -&gt; server  : cmd_string</p>
  <p>server -&gt; client  : ack_msg(06)</p>
  <p>server -&gt; client  : response_msg</p>
  </blockquote>
  <p>When I try to read the input either I'm only able to read to read just the acknowledgement message with one inputstream</p>
  <p>My client program is able to read the message somehow(hacky method). To read the input I have to read the ack msg using Bufferedreader.</p>
  <ol>
  <li>This buffered reader in only able to read ack_msg not the following msg</li>
  <li>DataInputstream code is needed to read response msg.</li>
  <li>If I skip step 1 and just use Datainputstream I'm not able to read complete messages.</li>
  </ol>
  <p>Client.java</p>
  <pre><code>try {
              Socket clientSocket = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);// ip,port
  
              System.out.println(&quot; client Socket created .. &quot;);
              
              PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
  
              String ToServer = command;
              outToServer.println(ToServer);
  
              in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  //          
              // while (!in.ready()) {
              // }
              System.out.println(in.read()); // Read one line and output it
  
              
              // ===
              byte[] messageByte = new byte[1000];//assuming msg size -need to know eact msg size ?
              boolean end = false;
              String dataString = &quot;&quot;;
              int bytesRead = 0;
  
              try {
                  DataInputStream in1 = new DataInputStream(clientSocket.getInputStream());
  //
                  while (!end) {
                      bytesRead = in1.read(messageByte);
                      dataString += new String(messageByte, 0, bytesRead);
                      if (dataString.length() &gt; 0) {
                          end = true;
                      }
                  }
                  System.out.println(&quot;MESSAGE: &quot; + dataString);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              // ===
                  
              in.close();             
  
              clientSocket.close();
              System.out.print(&quot;connection closed&quot;);
  
          } catch (Exception e) {
              System.out.print(&quot;Whoops! It didn't work!\n&quot;);
              e.printStackTrace();
          }
  </code></pre>
  <p><strong>Output</strong></p>
  <p>client Socket created ..</p>
  <p>response:6</p>
  <p>MESSAGE: ³CO³0³Œ</p>
  <p>connection closed</p>
  <p><strong>When I skip step 1</strong></p>
  <p>client Socket created ..</p>
  <p>MESSAGE: -</p>
  <p>connection closed</p>
  <blockquote>
  <p>How can I write code that reads all the input from
  server(acknowledgement msg &amp; response message &amp; it should also check
  the size of data and read accordingly)?</p>
  </blockquote>
  <p>Referred to <a href="https://stackoverflow.com/questions/19839172/how-to-read-all-of-inputstream-in-server-socket-java">How to read all of Inputstream in Server Socket JAVA</a></p>
  <p>Thanks</p>