title: java.io.EOFException when reading from socket
body: <p>I have a server and a client set up in this way. I can't find the cause of the EOFException, because it happens randomly. It throws the following exception every time a client connects, but I can't figure out the source of it. It always occurs before it reads what the client has sent. The exception always is at this line:</p>
  <pre><code>ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
  </code></pre>
  <p>Here is the exception:</p>
  <pre class="lang-java prettyprint-override"><code>java.io.EOFException
      at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2860)
      at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3355)
      at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:939)
      at java.base/java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:381)
      at com.denesgarda.Socketeer.data.End$3.run(End.java:62)
      at com.denesgarda.Socketeer.data.End$3.run(End.java:76)
      at com.denesgarda.Socketeer.data.End$3.run(End.java:76)
      at com.denesgarda.Socketeer.data.End.listen(End.java:83)
      at Server.&lt;init&gt;(Server.java:10)
      at SStart.main(SStart.java:5)
  </code></pre>
  <p>Here is my server code:</p>
  <pre class="lang-java prettyprint-override"><code>        if(listener == null) this.voidListener();
          ServerSocket serverSocket = new ServerSocket(port);
          End THIS = this;
          TimerTask timerTask = new TimerTask() {
              @Override
              public void run() {
                  try {
                      Socket socket = serverSocket.accept();
                      socket.setSoTimeout(10000);
                      Connection connection = new Connection(THIS, new End((((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace(&quot;/&quot;,&quot;&quot;)), port, listener);
                      try {
                          ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                          Object o = objectInputStream.readObject();
                          if (o.equals(&quot;01101100 01101001 01110011 01110100 01100101 01101110 00100000 01110011 01110100 01100001 01110010 01110100&quot;)) {
                              listener.event(new ConnectionEvent(connection));
                              listener.event(new ConnectionSuccessfulEvent(connection));
                          }
                          else {
                              listener.event(new ReceivedEvent(connection, o));
                          }
                          socket.close();
                      }
                      catch(EOFException e) {
                          e.printStackTrace();
                      }
                      this.run();
                  }
                  catch(Exception e) {
                      e.printStackTrace();
                  }
              }
          };
          timerTask.run();
  </code></pre>
  <p>Here is my client code:</p>
  <pre class="lang-java prettyprint-override"><code>        if(listener == null) this.voidListener();
          Socket socket = new Socket(address, port);
          Runtime.getRuntime().addShutdownHook(new Thread() {
              @Override
              public void run() {
                  try {
                      socket.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
          send(&quot;Message&quot;);
  
  
  
      public void send(Object object) throws IOException {
          Socket socket = new Socket(THAT.getAddress(), this.port);
          OutputStream outputStream = socket.getOutputStream();
          ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
          objectOutputStream.writeObject(object);
          socket.close();
      }
  </code></pre>
  <p><strong>What I've Tried</strong></p>
  <p>I've tried to fix this issue many times before. I tried to create object output streams. I've switched the order that I initialize the object input stream and object output stream. This is so that the server doesn't get frozen in a deadlock with the client. I have no idea what could be causing this error.</p>