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

时间:2015-11-12 18:15:27

标签: java serialization objectinputstream

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

非常感谢任何帮助或建议。谢谢:))

1 个答案:

答案 0 :(得分:2)

当您到达流的末尾时会抛出

EOFException,这会在对等方关闭连接时发生。

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

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