简体   繁体   English

Java网络中的EOFException

[英]EOFException in java networking

it is my first time using java networking. 这是我第一次使用Java网络。 I have to use it for a project i am working on which is a simple card game. 我必须将其用于我正在从事的项目,这是一个简单的纸牌游戏。 My server and client will communicate as the "Connection Received" does get printed. 我的服务器和客户端将进行通信,因为“连接已接收”确实会打印出来。 But after this line the following error occurs: 但是在此行之后,将发生以下错误:

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)
at ClientConnectionManager.reciveData(ClientConnectionManager.java:49)
at LogicThread.getHandSize(GameLogic.java:22)
at LogicThread.run(GameLogic.java:16)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
at LogicThread.getHandSize(GameLogic.java:22)
at LogicThread.run(GameLogic.java:16)
at java.lang.Thread.run(Unknown Source)

the classes in question are: 有问题的类是:

static ObjectInputStream input;
static ObjectOutputStream output;
static Socket socket;

    public static void connectToServer(){
    try {
        System.out.println("connecting");
        socket = new Socket(ip, port);
        input = new ObjectInputStream(socket.getInputStream());
        output = new ObjectOutputStream(socket.getOutputStream());
        output.flush();
        System.out.println("connected");
        reciveData();
        logic.startLogic();
        //listenForData();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static Object reciveData(){
    try {
        Object obj = input.readObject(); - Error is on this line
        System.out.println(obj);
        return obj;
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
    return null;
}

The server code for sending data: 用于发送数据的服务器代码:

    ServerSocket server;
    server = new ServerSocket(2302);
    Socket connection;
    connection = server.accept();
    output = new ObjectOutputStream(connection.getOutputStream());

    public void sendData(Object data){
    try {
        output.writeObject(data);
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I am not sure what causes this because the server sends a "Connection Received" which the client receives with no issue and prints. 我不确定是什么原因造成的,因为服务器发送了“接收到的连接”,客户端接收到的连接没有问题并可以打印。 But any call from outside the connectToServer() Method cause the error above. 但是从connectToServer()方法外部进行的任何调用都会导致上述错误。

The peer has closed the connection. 对等方已关闭连接。 Somewhere the server is closing the accepted socket or one of its streams. 服务器正在某个地方关闭可接受的套接字或其流之一。

That implies that if the server has really called sendData() it must have thrown an exception you haven't mentioned. 这意味着,如果服务器确实调用了sendData()则它一定引发了您未提及的异常。

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

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