简体   繁体   English

通过循环从套接字读取数据时获取“ java.io.EOFException”

[英]Getting “java.io.EOFException” when reading data from the socket over a loop

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... 现在,我正在尝试实施网络部分,以便启用2个播放器模式...

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(); 但是,当返回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 这里,“ in”和“ out”变量是我声明的类变量

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. 被这个问题困扰了很长时间...并迫切希望看到我的应用程序正常工作。

I think this is an off by one error, it might have to do with you flushing the buffer but then not writing or checking if it is empty before calling readUnshared(). 我认为这是一个错误,可能与您刷新缓冲区有关,但在调用readUnshared()之前不写入或检查其是否为空。

EOFException just said you reached the end of the stream or file (End Of File). EOFException只是说您到达了流或文件的末尾(文件末尾)。

I can't really debug it just with the code you posted. 我不能仅使用您发布的代码进行调试。 But make sure you are writing moves into wherever the "unshared" moves are being kept, or check, before calling the Stream. 但是在调用Stream之前,请确保将移动写入保留或检查“未共享”移动的任何位置。

EOFException is thrown when you call readObject() , readInt() , etc., on a socket that has no more buffered data to read and has been closed by the other end, just like readLine() returning null or read() returning -`. 在套接字上调用readObject()readInt()等时,在没有更多缓冲数据可读取并且已被另一端关闭的套接字上抛出EOFException ,就像readLine()返回null或read()返回- `。 It is a 'normal exception'. 这是“正常的例外”。 Just close the socket and forget about the connection. 只需关闭插座,而不必担心连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM