0

我正在开发一个多人蛇游戏。游戏似乎运行良好,但它经常(不是每次)随机抛出 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();
    }
}

实际上我不知道问题出在哪里,所以我可能倾倒了比必要更多的代码。

4

2 回答 2

0

经过几个小时的挖掘,我终于找到了解决问题的方法。在服务器端代码中,请参见以下代码:

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();
//rest of code goes here. Closing brackets as above.

如果我写这行

 objout = new ObjectOutputStream(map.get(name).getOutputStream());

在复合语句之前

if (name.player.equals(snake.player)) {
            name.copyValues(snake);
}

我似乎摆脱了 StreamCorrupted Exception。任何关于为什么会发生这种情况的建议都将受到欢迎。

于 2013-04-13T14:58:12.150 回答
0

我也遇到了与上面解释的相同的异常和问题,在写入(发送)msg(对象)时,我正在创建新的 OutputObjectStream,如下所示

//client or server side
while(true)
{
    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
    out.writeObject(some_msg_object);
}

/* but while reading(receiving), I was not creating new inputObjectReader
ie */
//on other side
while(true)
Message msgReceived = (Message)inputObjectReader.readObject();

这导致了完全相同的教育,

因此,如果发送 msg,通过创建新的 OutputStream,通过创建新的 InputStream 接收它,反之亦然,使用相同的已创建的 OutputStream 发送,通过已创建的 InputStream 接收.....

于 2014-02-23T23:09:46.640 回答