I'm just learning the network from a java book, so I'm a little noob. I could not find this problem in a book or on the Internet, so I decided to go online.
The book says to use ObjectOutputStream and ObjectInputStream to send and receive objects on different consoles.
Now I can successfully receive objects that I submit, but only once. When I send different objects: random strings, integer and nameless instances, the console has all the correct fields. But when I send an instance of an object, change the value of one of the fields in the instance and resubmit the object, then inpustream loads the original values ββof the instance.
So, say, for example, I had an instance of the class with the public int "var" equal to 1. If I send an instance of this class, the client will receive it and correctly report that var = 1. However, if I change var to 2 (in to the same instance) and resubmit it, the client will end the call to the read () method (so it should have received a new object!), but it will tell var as 1. If I send an instance to another client that has not yet received the instance, it will report var correctly as 2, and it will continue to report it as 2, even if I change var.
The fact that the client reads the correct version of the instance, if it has not received it earlier, should mean that the object is correctly sent through the output stream; for some reason, the input stream just doesn't work. It almost looks like it is the same object, so it assumes that it has the same values ββwithout validation. Why is this happening and how can I fix it?
Sorry if I ask something stupid - the book does not explain how serialization and sockets work, how to use them, so I could be completely confused about how to use them. Thank!
Simple code I wrote to test the problem:
Server: (has a timer action to continue sending updated objects)
public void actionPerformed(ActionEvent e)
{
object.var++;
output.write(object);
output.flush();
System.out.println(object.var);
}
Client
public void run()
{
while(true)
{
Test t = (Test)input.readObject();
System.out.println(t.var);
}
}
When these programs run the output for the Server class, it 1,2,3,4 ... grows infinitely, and the client output is only 1,1,1,1,1,1,1,1, etc.
, , . , , .
EDIT: , read() ( , echo), input.readObject()