ObjectInputStream在客户端服务器安装程序上引发EOFException



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

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

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

您需要单独捕获它,而不是将其视为错误。

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

最新更新