Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap over network via ObjectInputStream does not work properly?

 
Cedra Cascuel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also send your map with writeUnshared()
 
Cedra Cascuel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic