我现在在Android手机上创建一个简单的TicTacToe游戏。 我使用java服务器来处理多人部分,但是当我将玩家和'新'ObjectInputStream配对时,它会抛出异常。java.io.StreamCorruptedException:无效的流头:74001057
java.io.StreamCorruptedException: invalid stream header: 74001057
这是我的服务器代码时创建一个新的游戏主题:
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();
}
这是我的服务器代码在游戏线程接收消息:
// 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);
}
}
}
}
您应该在问题中发布相关代码(即在这种情况下发送和接收消息的代码),而不是链接到整个项目。这会让更多的人了解你的问题,你更有可能得到答案。 –
@KErlandsson编辑。 对不起,因为我虽然使用线程,但问题可能发生在其他地方,我不知道。 所以我张贴在这里~~ –
整个项目你可能想看看例如这样的问题:http://stackoverflow.com/questions/23262160/java-io-streamcorruptedexception-invalid-stream-header-54657374 –