title: Correct way to close the ObjectInputStream and ObjectOutputStream
body: <p>I am trying to make a class in which I can serialize and deserialize an object. The class should work in this way: I serialize the object and send it to server and then the server will modify one field of my object and send it back so I can deserialize the received object. But if I close the <code>OutputStream</code>, I get a socket write error. How can I close the <code>OutputStream</code> and the <code>InputStream</code> correctly?</p>
  
  <p><strong>EDIT:</strong>
  Even if I use try-with-resources, I still get the same error.</p>
  
  <p>Code:</p>
  
  <pre><code>public class UtilSerialize {
  InetAddress address;
  Socket socketConnection;
  
  public UtilSerialize() {
      try {
          this.address = InetAddress.getLocalHost();
          socketConnection = new Socket(address, 8000);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public void serializeObject() {
  
      try {
          OrdinaryUser user = new OrdinaryUser("Joe", "Joe");
          ObjectOutputStream clientOutputStream = new ObjectOutputStream(socketConnection.getOutputStream());
  
          clientOutputStream.writeObject(user);
  
          System.out.println(user.getUsername());
  
          //clientOutputStream.close();
  
      } catch (Exception e) {
          System.out.println(e);
      }
  }
  
  public void deSerializeObject() {
      try {
          ObjectInputStream clientInputStream = new ObjectInputStream(socketConnection.getInputStream());
          OrdinaryUser user = (OrdinaryUser) clientInputStream.readObject();
          System.out.println(user.getUsername());
          clientInputStream.close();
      } catch(Exception e)
      {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>This is the exception I'm getting and it is on the server side:</p>
  
  <pre><code>java.net.SocketException: Software caused connection abort: socket write error
  at java.net.SocketOutputStream.socketWrite0(Native Method)
  at java.net.SocketOutputStream.socketWrite(Unknown Source)
  at java.net.SocketOutputStream.write(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
  at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
  at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
  at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
  at java.io.ObjectOutputStream.writeObject0(Unknown Source)
  at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
  at java.io.ObjectOutputStream.writeObject(Unknown Source)
  at ro.unitbv.serverinvoicemanagement.classes.ServerThread.run(ServerThread.java:29)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>