title: Client Server EOF Error
body: <p>My application communicates between a server and a client using DataInputStream and DataOutputStream. The client will be requests from a website to my application, so I created a test client to test input that could come from the website.</p>
  
  <p>The method I decided to use would be that the client would send an integer first deciding the size of the data to be sent and then it would send the data. So, I was testing this method and it worked for the first string of data, but not the second and gave me an EOF error on the second try. I understand why it gave me an EOF because the client closed the connection, but I still have not read the incoming bytes, so the stream should still have more to read from.</p>
  
  <p>Client Code:</p>
  
  <pre><code>Socket socket = new Socket("127.0.0.1", 9000);
  DataOutputStream os = new DataOutputStream(socket.getOutputStream());
  os.writeInt("Test String".length());
  os.write("Test String".getBytes());
  os.flush();
  os.writeInt("String".length());
  os.write("String".getBytes());
  os.flush();
  os.close();
  socket.close();
  </code></pre>
  
  <p>Server Code: </p>
  
  <pre><code>byte[] data;
  
  try{
      DataInputStream input = new DataInputStream(client.getInputStream());
      DataOutputStream output = new DataOutputStream(client.getOutputStream());
          client.setSoTimeout(SOCKET_TIMEOUT);
  
      logger.log(Level.INFO, "Client connected with IP: " + client.getInetAddress());
  
      data = readNextPacket(input);
      if(!(new String(data).contains(PROTOCOL_HEADER)))
          throw new IOException("Invalid request from client.");
      else
          logger.log(Level.INFO, "Valid header: " + new String(data));
  
      data = readNextPacket(input);
      logger.log(Level.INFO, new String(data));
  }catch(IOException e){
      logger.log(Level.SEVERE, e.getMessage(), e);
  }finally{
      try {
          client.close();
      } catch (IOException e) {
          logger.log(Level.SEVERE, e.getMessage(), e);
      }
      logger.log(Level.INFO, "FINISHED");
  }
  </code></pre>
  
  <p>readNextPacket Method:</p>
  
  <pre><code>private byte[] readNextPacket(DataInputStream stream) throws IOException{
      int bytesToRead = stream.readInt();
      ByteArrayOutputStream os = new ByteArrayOutputStream();
  
      if(bytesToRead &lt;= MAX_BYTES){
          byte[] data = new byte[bytesToRead];
  
          while(stream.read(data) != -1 &amp;&amp; os.size() &lt; bytesToRead){
              os.write(data);
          }
      }
  
      return os.toByteArray();
  }
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>java.io.EOFException
  at java.io.DataInputStream.readInt(DataInputStream.java:392)
  at com.schmidt73.bidwriter.handlers.SocketHandler.readNextPacket(SocketHandler.java:1)
  at com.schmidt73.bidwriter.handlers.SocketHandler.parse(SocketHandler.java:16)
  at com.schmidt73.bidwriter.handlers.SocketHandler.start(SocketHandler.java:43)
  at com.schmidt73.bidwriter.BidWriter.main(BidWriter.java:21)
  </code></pre>