简体   繁体   English

如何正确停止serverSocket.accept()

[英]How to correctly stop serverSocket.accept()

I am writing a simple client/server chat program. 我正在编写一个简单的客户端/服务器聊天程序。 The server handles multiple clients in this way: 服务器以这种方式处理多个客户端:

public void start(int port)
{
    (new Thread(new Runnable()
    {
        @Override
        public void run() {
            try{
                serverSocket = new ServerSocket(port);
                SocketHandler handler;
                while(true)
                {
                    handler = new SocketHandler(serverSocket.accept());
                    handlersList.add(handler);
                    (new Thread(new SocketHandler(socket))).start();
                }
            }catch(IOException e)
            {
                for(SocketHandler handler:handlersList)
                    handler.close();
            }
        }
    })).start();
}

public void stop() throws IOException
{
    serverSocket.close();
}

Basically start() instantiates the ServerSocket and waits for clients to connect indefinitely. 基本上,start()实例化ServerSocket并等待客户端无限期地连接。 Whenever the user wants to close the server, the Server Socket is closed, which causes the accept() to fail and to throw and exception. 每当用户想要关闭服务器时,服务器套接字都会关闭,这会导致accept()失败并引发异常。 Then the catch(){} closes the various sockets created. 然后catch(){}关闭创建的各种套接字。 My questions are: 我的问题是:

  1. Do I have to close every socket created through serverSocket.accept()? 我是否必须关闭通过serverSocket.accept()创建的每个套接字?

  2. Is this the right way to stop such a server? 这是停止此类服务器的正确方法吗? That while(true) + use of exceptions for non-exceptional circumstances feels so wrong to me. 在非异常情况下使用while(true)+使用异常对我来说是非常错误的。 Is there a better way? 有没有更好的办法?

  3. Could I use an ExecutorService and then just call shutdownNow on it? 我可以使用ExecutorService然后在其上调用shutdownNow吗? Will the shutdownNow be able to stop the accept() call? shutdownNow是否可以停止accept()调用? Because the api doc states that it is not guaranteed to succeed. 因为api doc声明不能保证成功。

Please, feel free to point out any error/poor design choice that I've made. 请随意指出我所做的任何错误/较差的设计选择。 Ty

You can either close you connections manually (both client/server side) using the close() method or you can set a timeout . 您可以使用close()方法手动(在客户端/服务器端)关闭连接,也可以设置超时

Set a timeout on blocking Socket operations: 在阻止套接字操作上设置超时:

ServerSocket.accept(); ServerSocket.accept();
SocketInputStream.read(); SocketInputStream.read();
DatagramSocket.receive(); DatagramSocket.receive();

The option must be set prior to entering a blocking operation to take effect. 必须先设置此选项,然后才能进入阻止操作。 If the timeout expires and the operation would continue to block, java.io.InterruptedIOException is raised. 如果超时到期并且操作将继续阻塞,则引发java.io.InterruptedIOException。 The Socket is not closed in this case. 在这种情况下,套接字未关闭。


Is this the right way to stop such a server? 这是停止此类服务器的正确方法吗?

As you say it is a server that means you should not need to stop it but in exceptional conditions. 正如您所说的,这是一台服务器,这意味着您无需在特殊情况下停止它。


As you state the shutdownNow() API says: 当您声明shutdownNow()API时,会说:

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. 除了尽最大努力尝试停止处理正在执行的任务之外,没有任何保证。

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

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