正确关闭ObjectInputStream和ObjectOutputStream的方法

时间:2017-04-11 19:22:36

标签: java serialization deserialization objectinputstream objectoutputstream

我正在尝试创建一个可以序列化和反序列化对象的类。该类应该以这种方式工作:我序列化对象并将其发送到服务器,然后服务器将修改我的对象的一个​​字段并将其发回,以便我可以反序列化收到的对象。但是,如果我关闭OutputStream,我会收到套接字写入错误。如何正确关闭OutputStreamInputStream

修改 即使我使用try-with-resources,我仍然会遇到同样的错误。

代码:

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

这是我得到的例外,它在服务器端:

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)

2 个答案:

答案 0 :(得分:1)

  

但是如果我关闭outputStream,我会收到套接字写入错误。

你为什么要这样做?

这里:

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

clientOutputStream变量是指套接字的输出流 如果你关闭它,你关闭插座。

这是同样的事情:

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

clientInputStream变量指的是套接字的输入流 如果你关闭它,你关闭套接字。

虽然在读/写时需要套接字,但您不必关闭与之关联的流 否则套接字将不再处于可用状态,并且套接字上的读/写操作将失败。

答案 1 :(得分:0)

你可以尝试将它放在finally块中:

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

}