简体   繁体   English

Java:套接字关闭连接和ObjectInputStream

[英]Java: Socket close connection and ObjectInputStream

My code looks like this, this is Server code ( Runnable ) 我的代码如下所示,这是服务器代码( Runnable

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client...");
            this.socket = serverSocket.accept();
            System.out.println("Client accepted");
            while (true) {
                readCommand();
                try {
                    Thread.sleep(2);
                } catch (Exception e) {
                }
            }
        } catch (Exception ex) {
            Main.error("Server stopped for current host", ex);
        }
    }
}

My problem is: when the client closes the connection, he still waiting to read the command in readCommand(); 我的问题是:当客户端关闭连接时,他仍在等待读取readCommand();的命令readCommand(); until a command was sent and an EOFException will be thrown. 直到发送命令并抛出EOFException为止。 This is my readCommand method: 这是我的readCommand方法:

private void readCommand() throws Exception {
    Object o = new ObjectInputStream(socket.getInputStream()).readObject();
    if (o instanceof Command) {
        ((Command) o).execute();
        return;
    }
    if (o instanceof Recorder) {
        System.out.println("Recording received");
        ((Recorder) o).executeRecord();
        return;
    }
    if (o instanceof MyKeyEvent) {
        ((MyKeyEvent) o).execute();
        return;
    }
}

So I think before reading it has to check if the connection is open. 因此,我认为在阅读之前必须检查连接是否打开。

Edit: If I evaluate the run -method code, the EOFException is thrown in the try-catch-body . 编辑:如果我评估run方法代码,则在try-catch-body抛出EOFException But he stops accepting clients. 但是他停止接受客户。

An EOFException is exactly what I'd expect if the client closes the connection. 如果客户端关闭连接,则EOFException正是我所期望的。 So I'm not quite sure what your problem actually is . 所以我不太确定你到底什么问题。 Perhaps you need to distinguish between an expected client disconnect and an unexpected client disconnect. 也许您需要区分预期的客户端断开连接和意外的客户端断开连接。 In which case you should perhaps send some sort of End-of-message object to mark the end of transmission ? 在这种情况下,您也许应该发送某种End-of-message对象来标记传输的结束?

If you want to check if info is available, there's an available() method on the stream. 如果要检查信息是否可用,则在流上有一个available()方法。 I don't think you need your Thread.sleep(2) , btw, since I would expect a read on a valid input stream to hang in the absence of data. 我认为您不需要Thread.sleep(2) ,因为我希望在没有数据的情况下挂起对有效输入流的读取。

You can also "just" add another try-catch to catch just the EOFException 您还可以“仅”添加另一个try-catch以仅捕获EOFException

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client...");
            this.socket = serverSocket.accept();
            System.out.println("Client accepted");
            try {
                while (true) {
                    readCommand();
                    try {
                        Thread.sleep(2);
                    } catch (Exception e) {
                    }
                }
            } catch (EOFException ex) {
                System.out.println("client closed");
            }
        } catch (Exception ex) {
            Main.error("Server stopped for current host", ex);
        }
    }
}

sure, the End-of-command is much smarter/elegant... 当然,命令结束更聪明/更优雅...
[]] []]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM