Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (843)
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 1642 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:

   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) {

Which produces the following error:

4 invalid stream header
   at Source)
   at<init>(Unknown Source)
   at Client.ClientThread.<init>(

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


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:

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.

Pages: [1]
  ignore  |  Print  

EgonOlsen (42 views)
2018-06-10 19:43:48

EgonOlsen (22 views)
2018-06-10 19:43:44

EgonOlsen (43 views)
2018-06-10 19:43:20

DesertCoockie (197 views)
2018-05-13 18:23:11

nelsongames (124 views)
2018-04-24 18:15:36

nelsongames (123 views)
2018-04-24 18:14:32

ivj94 (863 views)
2018-03-24 14:47:39

ivj94 (124 views)
2018-03-24 14:46:31

ivj94 (768 views)
2018-03-24 14:43:53

Solater (140 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!