ObjectInputStream 在客户端-服务器设置上抛出 EOFException

ObjectInputStream throws EOFException on client-server setup

我正在创建客户端和服务器设置,以便向服务器发送命令,然后接收回复。但是,当我 运行 时,客户端抛出 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();
}

}

当我 运行 客户端时,它不断循环说 "Unable to get message" 永远。 但是,当我查看异常并退出代码(添加时)时,我遇到了这个问题:

 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)

如有任何帮助或建议,我们将不胜感激。谢谢:)

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

需要单独捕获,不要当成错误处理。

我还建议您将 System.exit(0); 更改为 break;