1

I am making a chess game application for a practice and finished up all the functionalities.

Now I am trying to implement the networking part so to enable 2 players mode...

In my server code I am using a loop to continuously get the data from both player turn by turn.

At the first turn of each player, the server works just fine and I confirmed the data is transferred correctly to the other player. But when it goes back to int[] data1 = (int[]) in1.readUnshared(); this very first line of the loop,

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)

the error occurs...

while(true){

    int[] data1 = (int[]) in1.readUnshared(); // THIS PART!!

    if(data1.length != 96)
        break;

    out2.writeUnshared(data1);
    out2.flush();

    int[] data2 = (int[]) in2.readUnshared();
    if(data1.length != 96)
        break;

    out1.writeUnshared(data2);
    out1.flush();
}

Here, "in" and "out" variables are class variables I declared

static ObjectInputStream in1;
static ObjectInputStream in2;
static ObjectOutputStream out1;
static ObjectOutputStream out2;

and I initilized them on the main function like this :

in1 = new ObjectInputStream(player1.getInputStream());

Is there any problem with the way I initialized streams...? been stuck for this problem for quite long... and desperate to see my application working.

4

2 回答 2

0

EOFException当您在没有更多缓冲数据要读取并且已被另一端关闭的套接字上调用readObject(),等时抛出,就像返回 null 或返回 -`。这是一个“正常的例外”。只需关闭套接字并忘记连接。readInt()readLine()read()

于 2012-06-14T05:34:58.140 回答
0

我认为这是一个错误,它可能与您刷新缓冲区但在调用 readUnshared() 之前不写入或检查它是否为空有关。

EOFException 只是说你到达了流或文件的末尾(End Of File)。

仅使用您发布的代码我无法真正调试它。但请确保在调用 Stream 之前将移动写入保留或检查“未共享”移动的任何位置。

于 2012-06-14T05:19:09.733 回答