ObjectInputStream & ObjectOutputStream

I just need some clarification of the order things have to be done when using ObjectStreams.

  1. Do u have to use is.flush() on the ObjectInputStream before you read in, or after u read in or both?

  2. Do u have to use os.flush() on the ObjectOutputStream before you send out, or after u send out or both?

  3. Do u have to use is.close() each time you finish reading in?

  4. Do u have to use os.close() each time you finish sending out?

Im asking this because when my client sends variable to the server, say x = 10, the server gets it and sends back the same value. The client then gets this variable back and prints it and it says “x = 10”.
Then the client sends a new value, x = 20, but the server just gets x = 10 and so send x = 10 back.
So basically my client/server setup works the 1st time, then after that, the server isnt really getting the new values from the client, its just receiving a packet from it but its not getting a new value form the ObjectInputStream. Im pretty sure its something to do with opening/closing ObjectInput/ObjectOutput streams.

You can not flush an InputStream. There is no flush() method.

Use flush after you have written all your data to the stream, before you grab the byte array from ByteArrayOutputStream (I assume that is what your are using).

You don’t have to, but it is a good idee to do so as it may release resources. It will also prevent you from using the stream later.

Same as 3)

It is likely that your problem lies elsewhere. Mixing the bytebuffer that you use to send/recieve somewhere. We could probably help you better if you could make a smal test case and post the code.

Thanks for your reply. I seem to have fixed it… i was trying to reuse the same ObjectOutStream in the clients send method so i guessed it wouldnt have a matching ObjectInputStream on the server considering the server was making new ObjectInputStreams each time, so i just make a new ObjectOutputStream each time now.
From 2 seperate clients, i can move 2 players around the screen and they move accordingly on each screen so its seems to have worked! :stuck_out_tongue:

There are some other limitations to consider when
using ObjectInputStream/ObjectOutputStream!

All the objects sent through ObjectInputStream/ObjectOutputStream
are internally stored in these objects. Let’s not go
into details. So, as long as you are sending new
data, all the objects are stored there for reference.

Unless you close ObjectInputStream/ObjectOutputStream
and set them to null, you will never be able to
garbage collect any of these objects.

Running your application over a longer period of
time without closing will give you a java.lang.RuntimeException, if you are constantly
sending data.

If you call reset() on your ObjectOutputStream it will reset the state of the cache. For instance if you have

class foo
{
public int a;
}

//server
foo f = new foo();
f.a = 1;
os.write(f);

//client
System.out.println(((foo) is.readObject()).a);
//prints out 1

//server again
f.a = 2;
os.write(f);

//on the client
System.out.println(((foo) is.readObject()).a);
//prints out 1

The problem is that the stream is smart enough to remember that it has already written f once, and when it writes f the second time, it just writes a reference to the object. This is what you want (when writing object graphs you dont want to create multiple copies of the same object.

If you wrote the code like

//server
foo f = new foo();
f.a = 1;
os.write(f);

//client
System.out.println(((foo) is.readObject()).a);
//prints out 1

//server again
f.a = 2;
os.reset();
os.write(f);

//on the client
System.out.println(((foo) is.readObject()).a);
//prints out 2

You will get what you want.