Serialize an Object over a socketTag(s): Language


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.


blog comments powered by Disqus