1

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3059427
  • 209
  • 2
  • 3
  • 10
  • EOFException - Signals that an end of file or end of stream has been reached unexpectedly during input. Source (http://docs.oracle.com/javase/7/docs/api/java/io/EOFException.html) – e.doroskevic Jan 03 '14 at 21:45
  • but what is causing this end of stream? shouldnt it just block and wait until an object is available. – user3059427 Jan 03 '14 at 21:48
  • I believe there will be an answer to this question shortly. It is my believe, however - if you run the method as you do 'player_reader.readObject();' if object is not available - it will throw this exception. Here is a reference to your problem - http://stackoverflow.com/questions/664331/when-will-an-eofexception-occur-in-javas-streams – e.doroskevic Jan 03 '14 at 21:52
  • @e.doroskevic No. Only at end of stream. 'Not available' just causes it to block. – user207421 Feb 07 '17 at 16:59

1 Answers1

0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483