Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 1063 times)
0 Members and 1 Guest are viewing this topic.
Offline tufflax

Junior Newbie





« Posted 2006-06-11 12: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 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (31 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!