(java) ObjectInputStream deserializing the wrong version of an object

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()

+5
3

, ObjectOutputStream , . , write , , .

, ObjectOutputStream.reset . .

, , , (.. A, B, A).

+12

read() readObject() ObjectInputStream? read(), .

public void run()
{
  while(true)
  { 
    Test t = (Test)input.readObject();
    System.out.println(t.var);
  }
}

EDIT: input.readObject();

+3

How many hours have I spent debugging this code. :)

I'm not sure what the right solution is for this, but if you create a new instance of the object or just clone it (), the code should work as expected before sending it.

It seems that the virtual machine recognizes the object that it receives as one that it already has, and just updates the pointer on the client, without worrying about checking if there are any changes.

to try ...

public void actionPerformed(ActionEvent e)
{
    object.var++;
    //assuming your object supports clone()
    output.write(object.clone());
    output.flush();
    System.out.println(object.var);
}
-1
source

All Articles