尝试执行 java 套接字操作时出现 java.io.EOFException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7666997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:55:05  来源:igfitidea点击:

java.io.EOFException when trying to perform java socket operations

javasocketsserialization

提问by coder9

I'm getting the following exception for the code included below that. This works fine when the while() loop is excluded. Why is this?

我收到下面包含的代码的以下异常。当排除 while() 循环时,这工作正常。为什么是这样?

Oct 6, 2011 1:19:31 AM com.mytunes.server.ServerHandler run
SEVERE: null
java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at com.mytunes.server.ServerHandler.run(ServerHandler.java:68)

Class ServerHandler:

类服务器处理程序:

public class ServerHandler extends Thread {
 .
 .
 .

public ServerHandler(...){
...
}


public void run(){

    try {

        os = s.getOutputStream();
        oos = new ObjectOutputStream(os);

        is = s.getInputStream();
        ois = new ObjectInputStream(is);

        while(true){

            msg = (Messenger) ois.readObject(); 

            String methodType = msg.getKey();

            //validating various data passed from the serialized object
            if(methodType.equals("validateCard")){

            } else if(methodType.equals("validatePIN")){

            }

        }

   } catch (SQLException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } finally{
        try {
            ois.close();
            is.close();
            oos.close();
            os.close();
            s.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



}

}

}

Class Server

班级服务器

public class Server{

    ...
    ServerSocket ss;
    ...

    public static void main(String args[]){
        Server server = new Server();
       server.init();

    }

    public void init(){
        try {
            System.out.println("Server started...");

            ss = new ServerSocket(port);

             System.out.println("Listening on " + ss.getInetAddress() + ":" + ss.getLocalPort());

            System.out.println("Waiting for clients...");

            while(true){
                  Socket incoming_socket = ss.accept(); // returns a Socket connection object if received
                  new ServerHandler(...).start();
            }

        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


 }

回答by Jon Skeet

You keep trying to read objects forever, never breaking out of the loop. When the client closes the connection, the stream will run out of data, and the ObjectInputStream.readObjectmethod will throw the exception you're seeing.

你一直试图永远读取对象,永远不会跳出循环。当客户端关闭连接时,流将耗尽数据,并且该ObjectInputStream.readObject方法将抛出您看到的异常。

How many objects did you expect to be in the stream, and why are you reading past the end of them?

您预计流中有多少对象,为什么要读到它们的末尾?