0

i have been debugging during the last hours over hours, but i did not get it....

i am sending an object from a Java Socket Server to a client (game server and game client), and it seemed that there was an issue while delivering the object....

server side:

//this is the controller function
public static void sendToAllPlayers(Object o){
    for (Connection c : NetworkService.getActiveConnections()){
        //i will explain this function below
        NetworkService.send(c, o);
    }
}

//this is the call to the controller
NetworkController.sendToAllPlayers(new SynchronizeMatch(MatchService.getMatchStore(), RoundService.getRoundStore()));

inside of the MatchStore ( => MatchService.getMatchStore()) there are all players listed. A player looks like:

class Player implements Serializable {
  private String username;
  private Integer points;
  ......
}

the issue was, that the field "points" was not delivered correctly from the server to the client. Client side:

Object object = in.readObject();
if (object instanceof SynchronizeMatch){
  SynchronizeMatch syncMatch = (SynchronizeMatch) object;
  MatchController.setMatchStore(syncMatch.getMatchStore);
  ......
}

so far everything worked fine - the match store (and the round store as well) kept all of the "live data" of the game, except the point level of the players. It was not updated correctly. And now the send() function:

public static void send(Connection c, Object o){
    try {
        c.getObjectOutputStream().writeObject(o);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

i post the answer below

messerbill
  • 5,499
  • 1
  • 27
  • 38

1 Answers1

0

after typing this question, i found the solution on the right:

public static void send(Connection c, Object o){
    try {
        c.getObjectOutputStream().writeObject(o);

        // !!!!!! THIS SOLVED MY PROBLEM !!!!!!

        c.getObjectOutputStream().reset();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

you need to reset the OutPutStream, otherwise it caches some stuff and you don't know if all of your data sets are actual. if you want to know more about resetting socket connections:

Is it ok to be calling reset() on an ObjectOutputStream very frequently?

i hope this will help the one or the other ;)

have a nice day, best regards,

Messerbill

Community
  • 1
  • 1
messerbill
  • 5,499
  • 1
  • 27
  • 38