views:

55

answers:

1

hello,

i am getting an excepetion when i try to read an object from an ObjectInputStream via network like this:

Socket socket = new Socket("localhost", 4444);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
while ((Object o = ois.readObject()) != null) {
}

Here is the exception I get:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)

The connection itself seems fine, since i am able to read strings via the network, but objects dont work.

Since it is an EOFExcepetion i assume that the stream currently sends no more data. (which is ok after the first object being sent). so i was thinking maybe i am using objectdeserialization the wrong way?

what could be the problem here? thanks!

+2  A: 

Why not call available() to check whether there's stuff to read ? It's probably worth publishing your server side code. At the moment we're seeing only one side of the story.

Brian Agnew
but is this accurate? what if only half of the objects binary data is received yet? it will crash as soon as it reaches the end again.
clamp
I would expect readObject() to block until all the required data is present (unless something exceptional has occurred). Don't forget that under the covers you have TCP looking after data integrity and completeness for you.
Brian Agnew
ok i tried available() and it always seems to return 0, which would explain the EOFException. but how can i debug this? i am definitely sending an Object to the client, so it should be there.
clamp
Did you publish the server side ? That would be of help. On the server side are you closing and/or flushing the connection ?
Brian Agnew
thanks! yes, i am flushing after the writeObject() on the server side. also i can see the message on the client side when checking the socket's inputstream with an InputStreamReader instead of an ObjectInputStream. so the message and the object is definitely arriving on the client.
clamp
ok sorry, i just found out there was indeed a problem on the server where it was closing the connection.
clamp