4 Replies Latest reply: Jun 2, 2010 12:45 PM by jtahlborn RSS

    deserialization / readObject problem

    843790
      Hi people

      I am in the making of a simple 2D multiplayer game.
      The server and clients are communicating through ObjectOutput/ Input streams (through Sockets).

      The server contains all the game logic, and it sends a serialized GameData class to the clients
      so they can draw the graphics accordingly to the events in the game.

      The problem is, the client only reads data from its objectinputstream successfully once, then it just doesnt work.
      It seems that after the first call of objectRead() it always just gets the same object with every call of objectRead.

      It is SURE that it the objectRead gets it successfully at least ONCE, because the client draws the graphics of the game after joining, but it doesn't move,
      it's like the client captures the state of the game in the moment of joining.

      There are no exceptions thrown, there are no problems on the serverside(it always writes out different GameData instances by writeObject(GameData)).

      I write the contents of the gd class every time on console after the call of GameData gd=(GameData) In. readObject();
      and it's always the same and hence, the drawn graphics are the same.
      I also write the contents of the sent GameData class every time on console after the call of out.writeObject( GameData instance); on the server side
      and it's always updated accordingly to the logic of the game.

      Please help, the deadline is close!
      while (true) {
                          // Get the game data from the server
                          try {
                               System.out
                                         .println("client: dataupdater class : trying to read data from server");                    
                               
                                              GameData gd = (GameData) In.readObject();               
      
      //at this point i write out the contents of gd to the console, and it's always the same!!
      //I spare you of that code.
      
                               setCurrentData(gd);
                               
                               System.out.println("Data read .");
                               
                          } 
                          catch (SocketException s) {
                               System.out.println("Disconnected from server.");
                          }
                          catch (IOException ie) {
                               ie.printStackTrace();
                          } 
                          catch (Exception e) {
                               System.out.println(e);
                          }
      
                          try {
                               Thread.sleep(15);
                          } catch (InterruptedException e) 
                               e.printStackTrace();
                          }
      
                     }
                }
      Also the serverside code, because it cant hurt.
           GameData gd = logic.exportGameData();
      
                //WRITING OUT THE CONTENTS OF GD, SAME CODE AS IN THE CLIENT
                      System.out.println("Number of entities: " + gd.entities.length);
                for (int i = 0; i < gd.entities.length; i++) {
                     Entity entity = gd.entities;
                     System.out.println(entity.getClass() + " PosX: " + entity.getX()
                               + " PosY: " + entity.getY());
                }

                // synchronized (outputStreams) {
                // For each client ...
                for (Enumeration<ObjectOutputStream> e = getOutputStreams(); e
                          .hasMoreElements();) {
                     
                     // ... get the output stream ...
                     ObjectOutputStream out = (ObjectOutputStream) e.nextElement();
                     // ... and send the message
                     try {
                          out.writeObject(gd);
                          out.flush();
                          
                     } catch (IOException ie) {
                          ie.printStackTrace();
                     }
                }
      Edited by: mostKeltem on Jun 2, 2010 9:47 AM