0

I am writing an application (a multiplayer game) where objects need to be passed from one instance of the application to another (and very quickly) via a server.

I am using ObjectOutputStreams and ObjectInputStreams obtained through the sockets to do this.

However, the state of the objects being passed is constantly being updated, meaning the typical writeObject() method won't do the trick (it only writes the object once, then passes a reference to it subsequent times it is called, meaning the values of its instance variables are never actually updated).

To "fix" this I have been calling the ObjectOutputStream.reset() method after every call to writeObject(), which does solve the problem of updating the object state; however, it runs much too slow, which I assume is a result of the objects being passed in their entirety each "refresh".

My question then is, how do I update the state of the object I am passing over the network without having to entirely rewrite the object? (I'm assuming this is how I would speed things up; if I'm wrong, or if other methods exist, please share) Thank you in advance for any advice!

Also, regarding the size of the object being passed: it isn't overly large... A couple of boolean variables, a couple of double arrays, and some other minor variables (No lists of length 1000 each containing another list).

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166

1 Answers1

1
  1. Use a BufferedOutputStream between the ObjectOutputStream and the socket.
  2. Use a BufferedInputStream between the ObjectInputStream and the socket.
  3. Call flush() after every writeObject() or writeUnshared().
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Ahhh! Thank you very much. This is exactly what I was looking for. I'll get around to testing it shortly and let you know if it solves my problem. – user1462665 Jun 18 '12 at 04:25