I'm writing a server <-> client application and I have a problem while sending Objects.
Generally, everything is fine as far as I don't want to send object back and forth. My problem by example:
- Server creates an object for instance myObject = new MyObject();
- Server puts an this object on a list myObjectList.add(myObject);
- Server sends the Object to the client via writeObject
- Client recives the package myObject = (myObject)input.readObject();
- Client sets a text in the object myObject.setText("Hello server");
- Client sends the object back to the server
- Server gets the object myObjectFromClient = (MyObject)input.readObject();
and here comes my problem out. Server will be reciving many such objects and he is going to check, which response did he get, but when I'll do a contains() check on a list of MyObjects: myObjectList.contains(myObjectFromClient); I'll get a false in result :(
Is there any way to recognize same objects when using sockets? one and obvious method is to set a field in an object with an ID and then I'll be able to recognize it when it gets back, but looks very non proffesional to me...
Thanks in advance
I don't know what you mean exactly by 'recognize the same objects', but if you're going to use List.contains(), it relies on the behaviour of the equals() method. So you can build in any behaviour you like, using any member data you like, via the equals() method.
By recognizing if the objects are the same I mean recognizing which object on the server side is the object that came back from the client (I've send the object earlier to the client).
Anyway, the idea you suggest seems to be my solution, I can set the ID's as fields in the object and than overload equals() method to compare ID's and everything should work fine with a minimum amount of changes. Thanks!