Forums Register Login

HashMap over network via ObjectInputStream does not work properly?

+Pie Number of slices to send: Send
Hello all

I'm currently trying to get a network application running. 2 instances from the same machine, one acting as Server, the other as Client. The Client continuously sends HashMaps of <String,Integer> type which have constantly changing values to the Server. It does this by an 'ObjectOutputStream(Socket.getOutputStream())', where it writes the object with writeObject(HashMap);, then flush() it. So far so good, right??

On the server end, it loops over and over collecting objects from an ObjectInputStream using readObject().

The problem is, it never, ever receives any updated HashMaps. It gets the first HashMap sent, and that's it. I can confirm this with System.out.println(readObject()); I cannot for the life of my understand why this is the case. Does anyone else?
+Pie Number of slices to send: Send
An ObjectOutputStream keeps track of which objects it has already sent, and doesn't send them more than once. Actually it's somewhat more complicated than that, but that's the general idea. If you want to send an object over again, after its state has changed, then there's a reset() method in ObjectOutputStream which you should call.
+Pie Number of slices to send: Send
You can also send your map with writeUnshared()
+Pie Number of slices to send: Send
Hello everyone

Just posting to say that reset() did the trick fine, thanks very much! I wish there was a "layman's version" of the descriptive text in the API documentation :/
I'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1887 times.
Similar Threads
readObject causing error
How to cast an Object to a HashMap with out getting unchecked cast warning
retrive serialize files data ERROR
Sockets objectStreamReader
Reading an object from ObjectInputStream
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:23:36.