2

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 OutputStream, I get a socket write error. How can I close the OutputStream and the InputStream correctly?

EDIT: Even if I use try-with-resources, I still get the same error.

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();
    }
}
}

This is the exception I'm getting and it is on the server side:

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)
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Octavian Marian
  • 65
  • 3
  • 11

2 Answers2

1

But if I close the outputStream I get socket write error.

Why do you want to do it ?

Here :

ObjectOutputStream clientOutputStream = new ObjectOutputStream(socketConnection.getOutputStream());

the clientOutputStream variable refers to the outputstream of the socket.
If you close it, you close the socket.

It is the same thing here :

ObjectInputStream clientInputStream = new ObjectInputStream(socketConnection.getInputStream());

the clientInputStream variable refers to the inputstream of the socket.
If you close it, you close the socket.

While the socket is required in reading/writing, you have not to close the streams associated to.
Otherwise the socket would be not any longer in a usable state and reading/writing operations on the socket will fail.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

You can try putting it in a finally block:

public void deSerializeObject() {
    ObjectInputStream clientInputStream = null;

    try {
        clientInputStream = new ObjectInputStream(socketConnection.getInputStream());
        OrdinaryUser user = (OrdinaryUser) clientInputStream.readObject();
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        clientInputStream.close();
    }

}
dspano
  • 1,540
  • 13
  • 25