aspose file tools*
The moose likes I/O and Streams and the fly likes HashMap over network via ObjectInputStream does not work properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "HashMap over network via ObjectInputStream does not work properly?" Watch "HashMap over network via ObjectInputStream does not work properly?" New topic
Author

HashMap over network via ObjectInputStream does not work properly?

Cedra Cascuel
Greenhorn

Joined: Dec 28, 2008
Posts: 2
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

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.
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
You can also send your map with writeUnshared()
Cedra Cascuel
Greenhorn

Joined: Dec 28, 2008
Posts: 2
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap over network via ObjectInputStream does not work properly?