-1

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);
      }
    }
  }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    You should post the relevant code (i.e. the code that sends and receives messages in this case) in your question, not link to your entire project. That will make more people understand your question and you are more likely to get an answer. – K Erlandsson Jun 02 '15 at 18:58
  • @KErlandsson edited. Sorry, because i'm though I'm using thread, the problem might occurred somewhere else that I don't know. So I post the whole project here~~ – Nicholas Siew Jun 02 '15 at 19:12
  • You might want to check out for example this question: http://stackoverflow.com/questions/23262160/java-io-streamcorruptedexception-invalid-stream-header-54657374 – K Erlandsson Jun 02 '15 at 19:19
  • Post the sending code. You aren't detecting end of stream correctly either. – user207421 Jun 08 '15 at 01:54

2 Answers2

0

Move the creation of the ObjectInputStream ahead of the loop. It tries to read a stream header placed there by the peer's ObjectInputStream, which you should likewise only create once. These streams should persist for the life of the socket, not be created anew for each send or receive.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

If you output the array of chars into ObjectOutputStream by separate chars, then you should read it also by chars, not as an array.

Also, check for consistent usage of ObjectOutputStream and ObjectInputStream. In your code on github you use DataInputStream in some places.

Also ensure that every wrapping of socket.getInputStream() with new ObjectInputStream() matches corresponding unwrapping of socket.getOutputStream() with new ObjectOutputStream(). Or better yet, wrap the socket streams in ObjectInputStream and ObjectOutputStream only once, not every time you want to read/write something.

Forketyfork
  • 7,416
  • 1
  • 26
  • 33