Note that there are some explanatory texts on larger screens.

plurals
  1. POSending the same objects with different fields over an object serialized stream
    primarykey
    data
    text
    <p>I have a client/server program, and I'm sending objects through <code>ObjectOutputStream</code> via <code>writeObject()</code> and <code>readObject()</code>. </p> <p>The object I'm sending is class consisting of a couple of fields, and another object inside (let's call the outer object <code>Wrapper</code> and the inner <code>Inner</code>. Both the custom objects implement <code>serializable</code>.</p> <p>The first time I send the <code>Wrapper</code> over, everything works flawlessly. Everything stored in both <code>Wrapper</code>, <code>Inner</code> and all the fields serialize and deserialize without problems.</p> <p>However, when the client then modifies the <code>Inner</code> class, puts it in the <code>Wrapper</code>, and sends it one more time, the <code>Inner</code> instance received by the server, is identical to the one received the first time around.</p> <p>My client: </p> <pre class="lang-java prettyprint-override"><code>Inner inner = new Inner(); inner.setValue("value"); ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); out.writeObject(new Wrapper(inner)); </code></pre> <p>Server:</p> <pre class="lang-java prettyprint-override"><code>ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream()); Wrapper wrapper = (Wrapper) in.readObject(); String value = wrapper.getInner().getValue(); </code></pre> <p>Client then modifies the <code>Inner</code> class (same instance as before) with a DIFFERENT string (ie. containing other letters then the first one):</p> <pre class="lang-java prettyprint-override"><code>inner.setValue("newValue"); out.writeObject(new Wrapper(inner)); </code></pre> <p>However, when I look at the <code>inner.getValue()</code> on the server, it has not changed and is still equal to <code>"value"</code>.</p> <p>I solved this, by making a hard copy of the inner class, before sending it over:</p> <pre class="lang-java prettyprint-override"><code>Inner newInner = new Inner(); newInner.setValue("newValue"); out.writeObject(new Wrapper(newInner)); </code></pre> <p>The new value now is updated as it should.</p> <p>Why does serialization work this way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload