Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Strange problem with Object streams

Started by tufflax Jun 10, 2006 at 9:12 PM 0 replies 350+ views
Original Post
tufflax
tufflax
Hey! I'm writing a little multiplayer game and it goes kind of well. But right now I'm having a problem. The problem is that when I'm using ObjectInputStream/OutputStream the Objects are not transfered correctly... I want to send a "ToServerPacket" to the server from the client. The ToServerPacket has only one instance variable (right now); a Position. The Position is a very simple class. It has only short x; and short y; and get/set methods. The ToServerPacket has a toString() which returns a String with the x and y values shown. Both ToServerPacket and Position implement Serializable. What happends is that when I send a ToServerPacket, I receive a ToServerPacket on the other side, so far so good, but the values (x and y in the Position) are not correct! Whatever Position I send, I get x==0 and y==0 when I read them on the other side. The way I tryed it was like this: ToServerPacket p = new ToServerPacket(...); System.out.println(p.toString()); out.writeObject(p); and Object o = in.readObject(); System.out.println(o.toString()); The toString() works but as I said the values after I read are always 0 and 0. Bummer... :( Edit: It appears that the values are not always 0 and 0, but instead equal to the values in the very first packet that was sent. Some code: http://rafb.net/paste/results/0ppUfA98.html Fixed: If I create a new Position and send that instead of referncing to the same old Position, it works. But why? :P [Edited by - tufflax on June 25, 2006 1:43:50 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.