【问题标题】:ObjectInputStream throws EOFException on client-server setupObjectInputStream 在客户端-服务器设置上抛出 EOFException
【发布时间】:2015-11-12 18:15:27
【问题描述】:

我正在创建客户端和服务器设置,以便向服务器发送命令,然后接收回复。但是,当我运行它时,客户端会抛出 EOFException。我知道这是文件结束异常,但我不确定我做错了什么,或者我该如何解决它。

服务器代码:

public class Server {

private ServerSocket server;
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;

//SET UP AND RUN SERVER
public void startRunning() {
    try {
        server = new ServerSocket(6789, 100);
        while (true) {
            try {
                waitForConnection();
                setupStreams();
                whileRunning();
            } catch (Exception e) {

            } finally {
                closeAll();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

//DURING RUN
private void whileRunning() throws IOException {
    String message = "You are now connected";
    sendMessage(message);
    System.out.println("Connected to client");

}

//WAIT FOR CONNECTION THEN DISPLAYS INFO
private void waitForConnection() throws IOException {
    System.out.println("Waiting for connection.....");
    connection = server.accept();
    System.out.println("Now connected to " + connection.getInetAddress().getHostAddress());
}

//SETS UP STREAMS
private void setupStreams() throws IOException {
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
}

//SEND MESSAGE TO CLIENT
private void sendMessage(String message) {
    try {
        output.writeObject("SERVER - " + message);
        output.flush();
        System.out.println("SERVER - " + message);
    }
    catch (Exception e) {
        System.out.println("ERROR: Can't send message");
    }
}

//CLOSE STREAMS AND SOCKETS
private void closeAll() {
    System.out.println("Closing Connections");
    try {
        output.close();
        input.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

服务器启动代码:

public class RunServer {

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

}

客户端代码:

public class Client {

private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;

public Client(String host) {
    serverIP = host;
}

public void startRunning() {
    try {
        connectToServer();
        setupStreams();
        whileRunning();
    }
    catch (Exception e) {

    } finally {
        closeAll();
    }
}

// CONNECT TO SERVER
public void connectToServer() throws IOException {
    System.out.println("Attempted connection...");
    connection = new Socket(InetAddress.getByName(serverIP), 6789);
    System.out.println("Connected to: " + connection.getInetAddress().getHostName());       
}

// SET UP STREAMS
private void setupStreams() throws IOException {            output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
    System.out.println("\nStreams Connected");
}

// WHILE CHATTING
private void whileRunning() throws IOException {
    do {
        try {
            message = (String) input.readObject();
            System.out.println(message);
        } catch (Exception e) {
            e.printStackTrace(); //In to view exception
            System.out.println("Unable to get message");
            System.exit(0); //In to stop it looping forever (Known issue)
        }
    } while (!message.equals("SERVER - END"));
}

// CLOSE STREAMS AND SOCKETS
public void closeAll() {
    System.out.println("Closing sockets, closing streams");
    try {
        output.close();
        input.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}   

}

启动客户端的代码:

public class RunClient {

public static void main(String[] args) {
    Client client = new Client("localhost");
    client.startRunning();
}

}

当我运行客户端时,它会不断循环说“无法获取消息”。 但是,当我查看异常并退出代码(如添加的)时,我遇到了这个问题:

 Attempted connection...
Connected to: localhost

Streams Connected
SERVER - You are now connected
java.io.EOFException
    Unable to get message
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at main.Client.whileRunning(Client.java:53)
    at main.Client.startRunning(Client.java:26)
    at main.RunClient.main(RunClient.java:7)

任何帮助或建议将不胜感激。谢谢:)

【问题讨论】:

  • 很可能抛出了一个异常,你在这里捕获了它​​:` } catch (Exception e) { }` 然后关闭了finally 块中的连接。永远不要抑制这样的异常;通常总是做错事。至少你想把它记录在某个地方,这样你就不会想知道发生了什么。如果您实际上无法处理异常,请不要捕获它。
  • 是的,我想抓住它以避免它使程序崩溃,我只是不知道如何处理它并正确地从服务器接收数据

标签: java serialization objectinputstream


【解决方案1】:

EOFException 在到达流的末尾时抛出,这发生在对等端关闭连接时。

你需要单独捕获它,而不是把它当作一个错误。

我也建议你把System.exit(0);改成break;

【讨论】:

  • 啊,好吧,您建议我如何处理它,以便在对等方关闭连接时停止尝试阅读消息?我有点困惑我会怎么做。顺便谢谢你的回答
  • 捕获异常并中断。
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 2016-04-10
  • 2012-05-30
  • 2020-05-03
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多