Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket Question  RSS feed

 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings

I have a strange bug that is driving me crazy

My application works fine in single user mode, however...

In network mode, I'm using a multi-threaded socket server

The client portion is a new thread spawned from the GUI,

this client thread waits until an object is received from

the GUI, wakes up, sends this serialized object to the server

via objectOutputStream.writeObject(message);
objectOutputStream.flush();

The server is waiting (objectInputStream) to read an incomming
message

When the server receives the message from the client, it figures

out what type of message (object) this is and calls the corresponding

remote data access wrapper method (with said message), which in turn calls the appropriate

local data access method, the results from the local data access method

are then attached to the message via setters, this message is then sent

back to the client.


The issue is this, in the FindRemote class I have a String array that

is initialized when the class in instantiated (from search criteria from the

search screen), this FindRemote instance is then sent to the server via
the client, (the criteria can be pretty complex), no matter how complex
the search criteria, the correct records come back, one the second and subsequent
attempts though, the same records that match the original criteria come back,

I attempted to debug by printing out the search criteria before just sending the message
to the server (Client objectOutputStream(message) and it is what is expected, on the server
side the first time thru right after the objectInputStream the criteria matches what was
sent from the client, however and subsequent tries, the client prints out the original search criteria.


I can even go and delete records, and then go back an search again, however the original search
criteria still comes up.


I'm going nuts, can anyone out there help?

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the magic of serialization is that you can send complex object graphs, even ones with loops or which contain multiple references to the same object, and only one copy of each object is sent, and the loops are broken. A given ObjectOutputStream will only send a given object once. If you try to send the same object again, it won't be sent; instead a note that says "Use that same object again" will be sent instead. That's what you're seeing here; the same String[] is "sent" multiple times, but it's really only snt once, and the changes are therefore not seen by the server.

So what to do? Use the ObjectOutputStream.reset() method after every distinct message you send. reset() forces the ObjectOutputStream to forget all the objects its ever sent before, and resend new copies when those objects are rewritten.
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it, thanks very much, I was bashing my head for three days!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!