我正在开发一个多人蛇游戏。游戏似乎运行良好,但它经常(不是每次)随机抛出 java.io.StreamCorrupted Exception 类型代码:AC 下面给出了相同的完整 StackTrace。
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at mycode.ConnectionManager.otherRunMethod(ConnectionManager.java:231)
at mycode.ConnectionManager$1.run(ConnectionManager.java:289)
at java.lang.Thread.run(Unknown Source)
生成错误的客户端代码如下:
void otherRunMethod() {
try {
while (true) {
boolean gameup = false;
objin = new ObjectInputStream(socket.getInputStream());
Object insnake = objin.readObject();
if (((Snake) insnake).player.length() >= 9) {
if (((Snake) insnake).player.substring(0, 8).equals(
"Resigned")) {
String[] s = ((Snake) insnake).player.split(",");
map.remove(s[1]);
continue;
} else if (((Snake)insnake).player.substring(0, 10)
.equals("food_eaten")) {
String[] s = ((Snake) insnake).player.split(",");
Game.foodx = Integer.parseInt(s[1]);
Game.foody = Integer.parseInt(s[2]) + 35;
objin = new ObjectInputStream(socket.getInputStream());
insnake = objin.readObject();
if(((Snake)insnake).score>highestscore){
highestscore=((Snake)insnake).score;
if(highestscore>=100 && highestscore<200)
levelUp(2);
else if(highestscore>=200){
levelUp(4);
}
}
if (s.length == 4) {
System.out.println(s[3]);
if (s[3].equals("game_up")) {
gameup=true;
}
}
}
}
Snake temp = new Snake((Snake) insnake);
map.remove(temp.player);
map.put(temp.player, temp);
if(temp.gameover){
allPlayersEliminated();
}
findPos();
game_UpCheck(gameup);
Thread.sleep(10);
}
} catch (java.net.SocketException s) {
JOptionPane.showMessageDialog(null, "Server Closed", "ERROR",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
对应的服务器端代码如下:
void relay(Snake move) {
boolean foodeaten = false;
Snake snake = move;
try {
if (move != null && move.player.length() > 9) {
if (move.player.equals("food_eaten")) {
move.player = move.player + ","
+ (Snakeserver.randomGenerator.nextInt(100) * 6)
+ ","
+ (Snakeserver.randomGenerator.nextInt(100) * 6);
foodeaten = true;
snake = (Snake) objin.readObject();
int rows = Snakeserver.score.getModel().getRowCount();
int row = 1;
for (int i = 0; i < rows; i++) {
if (((Snake) snake).player
.equals((String) Snakeserver.score.getModel()
.getValueAt(i, 0))) {
row = i;
break;
}
}
if (snake.score >= 300)
move.player += "," + "game_up";
Snakeserver.score.getModel()
.setValueAt(snake.score, row, 1);
}
}
if (move.gameover) {
int rows = Snakeserver.score.getModel().getRowCount();
int row = 1;
for (int i = 0; i < rows; i++) {
if (snake.player.equals((String) Snakeserver.score
.getModel().getValueAt(i, 0))) {
row = i;
break;
}
}
Object temp = Snakeserver.score.getModel().getValueAt(row,
1);
temp += " GAME OVER";
Snakeserver.score.getModel().setValueAt(temp, row, 1);
}
for (Snake name : map.keySet()) {
if (name != null) {
if (name.player.equals(snake.player)) {
name.copyValues(snake);
}
objout = new ObjectOutputStream(map.get(name)
.getOutputStream());
objout.writeObject(move);
objout.flush();
if (foodeaten) {
objout = new ObjectOutputStream(map.get(name)
.getOutputStream());
objout.writeObject(snake);
objout.flush();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
实际上我不知道问题出在哪里,所以我可能倾倒了比必要更多的代码。