简体   繁体   English

Java:正确关闭多线程服务器的套接字

[英]Java: properly closing sockets for multi threaded servers

I'm trying to create a multi threaded server to which multiple clients can connect and can be served.我正在尝试创建一个多线程服务器,多个客户端可以连接到该服务器并可以为其提供服务。 However, I'm not sure on how to properly free up my resources should the need arise.但是,如果需要,我不确定如何正确释放我的资源。

My server runs an input thread (waiting for user inputs) and a procressing thread (handles connections and users).我的服务器运行一个输入线程(等待用户输入)和一个处理线程(处理连接和用户)。 I open up a ServerSocket in the server class and pass it to my processing thread.我在服务器类中打开一个 ServerSocket 并将它传递给我的处理线程。 It looks like this:它看起来像这样:

public class ClientConnector implements Runnable {

private ServerSocket serverSocket;

public ClientConnector(ServerSocket serverSocket) {
    this.serverSocket = serverSocket;
}

@Override
public void run() {
    ExecutorService tcpExecutor = Executors.newCachedThreadPool();

    while (!serverSocket.isClosed()) {
        Socket clientSocket = null;

        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("could not accept connection");
        }

        if (clientSocket != null) {
            tcpExecutor.execute(new ClientHandler(clientSocket);
        }           
    }   
}
}

If I want to exit, I just run this method in my server class to close the ServerSocket:如果我想退出,我只需在我的服务器类中运行这个方法来关闭 ServerSocket:

public void exit() {
    try {
        serverSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Which should cause the next serverSocket.accept() call to throw an exception, and the loop stops since the socket is closed.这应该会导致下一个 serverSocket.accept() 调用抛出异常,并且由于套接字关闭,循环停止。

My question is - does closing a ServerSocket implicitly close ClientSockets that were created using it?我的问题是 - 关闭 ServerSocket 是否会隐式关闭使用它创建的 ClientSockets? Or should I make sure to close every single open ClientSocket by hand?或者我应该确保手动关闭每个打开的 ClientSocket? If so, is there an easy way to do so instead of saving every single connection made to the server somewhere?如果是这样,是否有一种简单的方法可以做到而不是将与服务器的每个连接都保存在某处?

does closing a ServerSocket implicitly close ClientSockets that were created using it?关闭 ServerSocket 是否会隐式关闭使用它创建的 ClientSockets?

No, it has no effect on them.不,这对他们没有影响。

Or should I make sure to close every single open ClientSocket by hand?或者我应该确保手动关闭每个打开的 ClientSocket?

Yes, and you should be doing that anyway, in every handler thread.是的,无论如何您都应该在每个处理程序线程中这样做。

If so, is there an easy way to do so instead of saving every single connection made to the server somewhere?如果是这样,是否有一种简单的方法可以做到而不是将与服务器的每个连接都保存在某处?

Just impose a read timeout and close each socket that times out.只需施加读取超时并关闭超时的每个套接字。 This is a normal part of any server.这是任何服务器的正常部分。 You don't have to collect the sockets or take any special measures about them for shutdown.您不必收集套接字或对其采取任何特殊措施来关闭。

No, it doesn't.不,它没有。 It just closes the ServerSocket meaning new connections can't be made.它只是关闭ServerSocket意味着无法建立新连接。 The existing open sockets will still keep working.现有的开放套接字仍将继续工作。

There's no "easy" way to close all the open sockets because it's not really hard to begin with (or if you find that as a hard part in network programming, there's something very strange going on).没有“简单”的方法可以关闭所有打开的套接字,因为开始并不难(或者,如果您发现这是网络编程中的难点,那么会发生一些非常奇怪的事情)。

让客户端处理程序线程在处理结束时关闭客户端套接字。

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

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