Jump to Real's How-to Main page

Serialize an Object over a socket

From the client side, you open a Socket and then
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(someObject);
On the server side, you use the ObjectInputStream.readObject method.

NOTE:
If the OutputStream is kept open and you modify your object and resend it, you will not see a change on the server side. That's because Java keeps objects sent in an internal cache and the old version will be taken in account instead of the new one. The fix is to do an ObjectOutputStream.reset() before re-sending the object or open a new connection each time.


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]