0

I am writing a game that should be played online. I have a server that starts new Thread every time a client connects. Then the client communicates with this Thread using ObjectInputStream and ObjectOutputStream. In this communication the client can start a new game or choose existing game to join. If a client joins existing game, server starts a new Thread with the game instance and this game instance should start its own communication with clients. I pass the Sockets to this game instance but when I try to send some message to clients, only one of them recieves it correctly. The second client throws an exception java.io.StreamCorruptedException: invalid stream header: 79ACED00. I use the same method to send the message, so I have no idea, why it works for one client and why it does not work for the second client.

Here is the code where the game instance is created:

GameInstanceDescription joined_game = (GameInstanceDescription) response;
joined_game.setSocket_challenger(socket_from_client);
joined_game.setChallenger(rcvd_msg.getChallenger());
joined_game.setSocket_promoter(game_server.getPromotersSocket(joined_game.getName()));
game_server.startNewGame(joined_game);

After the last line that Thread should end.

Here is the server method:

public void startNewGame (GameInstanceDescription desc)
{
    Thread t = new Thread(new MainGame(desc));
    t.start();
}

Here is the method used to send message from the game instance to the client:

public void sendMessageToClient(ServerToClientMessage message, ObjectOutputStream stream)
{
    try {
        stream.writeObject(message);
        stream.flush();
        stream.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And here is the clients code which should read the message from the server, but it works only for one client and also the exception is thrown here:

input = new ObjectInputStream(client.getInputStream());
Object o = input.readObject();

I didn´t find anything useful on the internet what would solve this, so could you please tell me what is wrong? Thanks in advance.

Sefinek
  • 29
  • 1
  • 3
  • 10

1 Answers1

1

Don't create a new ObjectInputStream every time you read an object (or a new ObjectOutputStream every time you write one). Use the same streams for the life of the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Is it possible to pass these Streams to another `Thread`? Because I think I tried this but it also ended up with invalid stream header exception. But it is possible there was some other mistake. – Sefinek Mar 15 '15 at 18:08
  • You will only get this message when you construct an `ObjectInputStream.` Multi-threaded use is problematic and must be synchronized, but it won't cause this problem. – user207421 Mar 15 '15 at 23:31
  • The problem is solved, I rewrote some parts of my program and it works now. I pass Streams to another Thread instead of creating new ones and I also changed some other things I wasn´t doing right and it is ok now. Thanks for help.. – Sefinek Mar 17 '15 at 11:48
  • This helped me as I was creating an object stream everytime before I read from the socket's stream – Richard Barker Jul 10 '15 at 04:27