I'm now creating a simple TicTacToe game on Android phone. I used java server to handle multiplayer part, but when I pair the players and 'new' the ObjectInputStream, it throw me the exception.
java.io.StreamCorruptedException: invalid stream header: 74001057
These is my server code when create a new game thread:
public GameThread(Socket Player1, Socket Player2) {
this.Player1 = Player1;
this.Player2 = Player2;
System.out.println("GameThread Started!");
//Exception throw at the codes below
new ReceiveMessagesThread(this.Player1, this.Player2).start();
new ReceiveMessagesThread(this.Player2, this.Player1).start();
}
These is my server codes for receiving message in a game thread:
// This is an inner class.
private class ReceiveMessagesThread extends Thread {
private Socket SourceClient, DestinationClient;
ReceiveMessagesThread(Socket SourceClient, Socket DestinationClient) {
this.SourceClient = SourceClient;
this.DestinationClient = DestinationClient;
}
@Override
public void run() {
while (true) {
try {
// Exception throw at the line below
ObjectInputStream in = new ObjectInputStream(this.SourceClient.getInputStream());
switch (in.readByte()) {
case ServerGlobal.BOARD_STATUS:
GameBoard = (char[][]) in.readObject();
SendBoardStatus(this.DestinationClient);
break;
}
}
catch (java.io.StreamCorruptedException ex) {
ex.printStackTrace();
break;
}
catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(GameThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}