Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Strange problem with Objects streams  (Read 892 times)
0 Members and 1 Guest are viewing this topic.
Offline tufflax

Junior Newbie





« Posted 2006-06-11 14:38:42 »

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... Sad

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
Offline CommanderKeith
« Reply #1 - Posted 2006-06-11 15:16:45 »

This is the mystifying problem everyone using ObjectIn/OutputStreams has to have.  And after all of the pain trying to figure out what's wrong, you'll never forget the solution.  What is happening is that after the first write, all writes to that same object are sent as a reference only even though you changed the original object. 

After out.writeObject(p) call out.reset() and it will be apples.   Wink

Good luck.

(Alternatively,
1.  you can send a new object each time, but that's messy since any Object fields of that new Object need to be new as well, etc. 
2.  create a new ObjectOutputStream for each new send, but make sure you create a new ObjectInputStream to recieve it as well, there must be this symmetry.  This makes more work for the Garbage COllector though & each write(obj) will produce more bytes than by having a single stream that's re-used.
You're better off overall by just calling reset().  Smiley  )

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (65 views)
2013-05-17 21:29:12

alaslipknot (74 views)
2013-05-16 21:24:48

gouessej (105 views)
2013-05-16 00:53:38

gouessej (102 views)
2013-05-16 00:17:58

theagentd (112 views)
2013-05-15 15:01:13

theagentd (102 views)
2013-05-15 15:00:54

StreetDoggy (147 views)
2013-05-14 15:56:26

kutucuk (169 views)
2013-05-12 17:10:36

kutucuk (168 views)
2013-05-12 15:36:09

UnluckyDevil (177 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.081 seconds with 20 queries.