title: SocketException: Connection reset on server with ObjectInputStream
body: <p>
  I'm trying to get my head around the <em>ObjectInputStream/ObjectOutputStream</em>, so I create a very simple server-client application where the client sends a HashMap object over the created stream and the server receives that and prints it out.</p>
  
  <p>This my server code:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  import java.net.*;
  
  public class Server {
  
      public static void main(String[] args) throws IOException, ClassNotFoundException {
          ServerSocket server = new ServerSocket(4444);
          while (true) {
              Socket socket = server.accept();
              ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
  
              if (objIn.readObject() != null) {
                  System.out.println(objIn.readObject());
              }
          }    
      }
  }
  </code></pre>
  
  <p>This my client code:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.util.HashMap;
  
  public class Client {
  
      public static void main(String[] args) throws IOException {
          Socket sock;
          int port = 4444;
          HashMap&lt;Integer, String&gt;  mapSend= new HashMap&lt;&gt;();
          mapSend.put(1,"row1");
          mapSend.put(2,"row2");
  
          sock = new Socket(InetAddress.getLocalHost(), port);
          ObjectOutputStream objOut = new ObjectOutputStream(sock.getOutputStream());
  
          objOut.writeObject(mapSend);
          objOut.flush();
      }
  }
  </code></pre>
  
  <p>I run the server file and it runs ok.<br>
  Then I run the client file and I get the following error on the server:</p>
  
  <pre class="lang-java prettyprint-override"><code>    Exception in thread "main" java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:189)
      at java.net.SocketInputStream.read(SocketInputStream.java:121)
      at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
      at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2716)
      at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2740)
      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1978)
      at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:499)
      at java.util.HashMap.readObject(HashMap.java:1115)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at iotest.Server.main(Server.java:17)
  Java Result: 1
  </code></pre>
  
  <p>P.S. I don't know if I have to do something to serialize the HashMap, but I think I saw somewhere that this is handled internally by Java. I might tough be wrong.</p>
  
  <p><strong>EDIT after Aaron answer</strong>
  I implemented this part in my server as Aaron suggested:</p>
  
  <pre class="lang-java prettyprint-override"><code>Object objRead=objIn.readObject();
              if (objRead != null) {
                  System.out.println(objRead);
              }
  </code></pre>
  
  <p>and now I get this error,again from server,after I run the client:</p>
  
  <pre class="lang-java prettyprint-override"><code>Exception in thread "main" java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:189)
      at java.net.SocketInputStream.read(SocketInputStream.java:121)
      at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
      at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2716)
      at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2740)
      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1978)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at java.util.HashMap.readObject(HashMap.java:1154)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at iotest.Server.main(Server.java:17)
  Java Result: 1
  </code></pre>