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.