Why is ObjectInputStream readObject() throwing EOF exception

天涯浪子 提交于 2019-12-02 09:17:34

问题


I am stuck with this very strange problem. In the client I am passing in objects like

try{
    oos.writeObject(new GameStartSerializedObject());
    oos.flush();
}
catch(Exception e){
    e.printStackTrace();
}

and in the server I am reading the object

try{
    //Its my turn
    thrown_message = player_reader.readObject();
}
catch(Exception e){

My question is why am i getting EOF exception. My understanding of object input stream is when i call readObject() i should block until i get an object so how does it know if the eof is reached? Please help!

This is how I create object streams

ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();

Also, after i write object and flush should i close the stream. I am not closing it since the objects are written pretty regularly from different parts of the code one after another.


回答1:


The peer has closed the connection. Ergo there are no more objects to read. Ergo you have reached the end of the stream. Ergo readObject() throws EOFException.



来源:https://stackoverflow.com/questions/20913190/why-is-objectinputstream-readobject-throwing-eof-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!