在java中使用readObject()方法时出现EOFException

时间:2014-03-29 13:52:29

标签: java

我正在尝试从服务器读取消息。以下是一段代码:

try{

    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());
    stdIn = new BufferedReader(
                new InputStreamReader(System.in));

    J = new JChatComm(out , in,"client","server");

    String userInput = "Free for a chat?";

    JPacket p = new JPacket(userInput,"client");
    out.writeObject(p);


    p = (JPacket)in.readObject();


    if (!p.message.equals("Sure. Let us begin.")){
        System.out.println("Server seems to be unavailable.");
        socket.close();
    }
    else{
        System.out.println("Chat Initiated..");
    }
}
catch(Exception e){
    e.printStackTrace();
}

但是它提供了以下输出:

java.io.EOFException
at        
java.io.ObjectInp utStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2596)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1317)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at JClient.callServer(JClient.java:35)
at jtalkG24.main(jtalkG24.java:18)

第35行是具有readObject()方法的行。 我无法弄清楚我哪里出错了。

1 个答案:

答案 0 :(得分:0)

你不会出错。你已经到达了流的末尾。这就是异常的含义。你必须抓住它并作出相应的反应。