DoubleDouble
DoubleDouble

Reputation: 1493

Java Serialization through Network, repasses same objects?

I'm going to layout the situation and the question is at the bottom.

I've got an object like so:

public class GameObject implements Serializable{
/**
 * 
 */
    private static final long serialVersionUID = 3081889342394131807L;
    public float x, y, vx, vy;
    public int size;
    public int mass;

    public GameObject()
    {
        Random rand = new Random();
        x = rand.nextInt(400);
        y = rand.nextInt(400);
        size = rand.nextInt(10);
        mass = size * size * size;
        vx = 0.3f;
        vy = 0.3f;
    }

    public void updateGameObject()
    {
        x+=vx;
        y+=vy;
    }
}

I've got a client-server setup using sockets: when the server starts up it creates a GameObject and adds it to a HashMap. Then the server loops, updating the object and sending the updated object. connection is a standard socket.

while (true)
{
    gameObject.updateGameObject();
    try {
        if (objOutputStream == null)
            objOutputStream = new ObjectOutputStream(connection.getOutputStream());
        objOutputStream.writeObject(o);
        objOutputStream.flush();
    } catch (Exception e) { e.printStackTrace(); }
}

On the client side, I simply read the object and draw it to the screen. This is where the problem happens. I am definitely receiving an object with each loop, but it seems the fields of the GameObject are not changing when I read it.

while (true)
{
    try {
            if (objInputStream == null)
                objInputStream = new ObjectInputStream(connection.getInputStream());
            return objInputStream.readObject();
        } catch (Exception e) { e.printStackTrace(); }
}

objInputStream and objOutputStream are both initialized above the while loops.

If I do a println() on the x and y values of the object I'm getting on the client, the values are never changing from their initial value.

When I do a println() on the x and y values on the server just before sending it, it prints out the correct values. Something like so:

Sending Object with 1, 1
Received Object with 1, 1
Sending Object with 2, 2
Received Object with 1, 1
Sending Object with 3, 3
Received Object with 1, 1

I feel like there is something basic I am missing. Though when I try finding other questions with this problem I can't seem to find one. Maybe one of you can provide me with some insight?

Upvotes: 0

Views: 249

Answers (2)

pobrelkey
pobrelkey

Reputation: 5973

ObjectOutputStream tries to make serializing complex graphs of objects painless by not re-writing to the stream any object you've written to it previously (as determined by referential equality, not equals()).

A lazy solution would be to implement Cloneable and clone() your object before sending - but unless you're on a thoroughly ancient JDK, you'll want to call writeUnshared() instead, as it will write a "fresh" copy of your object rather than a reference to a previous version.

Upvotes: 1

user207421
user207421

Reputation: 310957

Use either ObjectOutputStream.writeUnshared() instead of writeObject(), or call reset() whenever you want to start again with a new set of values.

Upvotes: 3

Related Questions