title: Java program throwing EOFException in socket programming even after outputstream is flushed
body: <p>Here is the complete stacktrace of the error </p>
  
  <pre><code>java.io.EOFException
      at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
      at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:800)
      at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:298)
      at mypackage.JChatComm.receiveMessage(JChatComm.java:83)
      at mypackage.JThread.run(JThread.java:19)
      at java.lang.Thread.run(Thread.java:701)
  </code></pre>
  
  <p>Here is my receiveMessage code:
  This function creates a ObjectInputStream of a object JPacket and print that object.
  But after one iteration, it is throwing EOFException and printing 4,11,8 and stackTrace.</p>
  
  <pre><code>public boolean receiveMessage() throws IOException, ClassNotFoundException{
          try{
              System.out.println("4");
          InputStream inFromServer = client.getInputStream();
          System.out.println("11");
          ObjectInputStream in = new ObjectInputStream(inFromServer);
          //System.out.println("hellow owrol");
          //System.out.println("5");
          final JPacket jp;
           jp = (JPacket) in.readObject();
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  System.out.println("6");
                  if (type.equals("s")){
                      tab.ta.append("Client: "+jp.msg+"\n");
                  }
                  else {
                      cp.ta.append("Server: "+jp.msg+"\n");
                  }
  
              }
          });
  
          System.out.println("other: "+jp.msg+ "     Sent on:" + jp.a1);
          //System.out.println("msg got");
          return true;
      }
      catch (Exception ea){
          System.out.println("8");
          ea.printStackTrace();
          return false;
      }
  
      }
  </code></pre>
  
  <p>and here is my sendMessage Program
  This programs is the main program to send the message. It is working fine always and properly flushing the outputstream. But still receiveMessage is throwing error.</p>
  
  <pre><code>public boolean sendMessage() throws IOException{
          try{
              System.out.println("3");
  
          final String msg;
          if (type.equals("c")){
              msg = cp.tf.getText();
          }
          else msg = tab.tf.getText();
          System.out.println(msg);
          JPacket j1 = new JPacket(msg);
          OutputStream outToServer = client.getOutputStream();
          ObjectOutputStream out = new ObjectOutputStream(outToServer);
          out.writeObject(j1);
          System.out.println("13");
          out.flush();
          System.out.println("12");
  
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  if (type.equals("s")){
                      tab.ta.append("You: "+msg+"\n");
                      tab.tf.setText("");
                  }
                  else {
                      System.out.println("Client");
                      cp.ta.append("You: "+msg+"\n");
                      cp.tf.setText("");
                  }
              }
          });
          if(msg.equals("End Chat")) {
              endChat();
              return false;
          }
          else
              return true;
      }
      catch (Exception ea){
          ea.printStackTrace();
          System.out.println("7");
          return false;
      }
  }
  </code></pre>
  
  <p>I am flushing the object output stream everytime but still it throwing the same error. </p>