Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Object Stream Question / Problem  (Read 1093 times)
0 Members and 1 Guest are viewing this topic.
Offline churchill

Senior Newbie





« Posted 2006-06-20 22:52:10 »

I am currently writing some software which uses ObjectInput/ObjectOutputStream to pass messages back and forth.

I have a class called Message, and a class called GameState, both are serializable

Inside Message, there is a GameState object, which holds the state of a current game.

I construct a GameState, then pass it into the constructor of Message, and set my pointer to point to that GameState.

I then try to send Message across an ObjectOutputStream to the client from the server.

Here is where a problem starts... I try to set up the stream in a thread on the client side with the following code:

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public ClientThread(Socket conn, GameClient gcl) {
     
      connection = conn;
      gc = gcl;
     
      try {
         out = new ObjectOutputStream(connection.getOutputStream());
   (36)      in = new ObjectInputStream(connection.getInputStream());
         
      } catch (Exception e) {
         e.printStackTrace();
      }
   }


Which produces the following error:

1  
2  
3  
4  
java.io.StreamCorruptedException: invalid stream header
   at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
   at java.io.ObjectInputStream.<init>(Unknown Source)
   at Client.ClientThread.<init>(ClientThread.java:36)



My two questions are this:

1) Can you pass an object through an OOS which contains a reference to another object, and retrieve both on the other side, for example:

GameState g = new GameState(args);
Message m = new Message(g);     // this will simply contain a link to g in a private variable

OOS.writeObject(m);


then on the client:

Message m1 = (Message)(OIS.readObject());
GameState g1 = m1.getGameState();

Or does that fail completely somehow?


2) What could be causing that error? I have never encountered it before and dont know where to turn at all...
Offline CommanderKeith
« Reply #1 - Posted 2006-06-21 06:46:35 »

See this thread:  http://www.java-gaming.org/forums/index.php?topic=14035.0

For the exception, my guess is that you're doing multiple writes with the one ObjectOutputStream but reading it with different ObjectInputStreams, or vice versa. 

The other thing that can cause that error is when the bytes made by ObjectOutputStream sent over the network don't come out the same on the other side.  Or you a writing to the one ObjectOutputStream (or sending using the one Socket) with overlapping threads which causes the bytes to get muddled up. 

Post some code if this hasn't helped.
Keith

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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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!