Java:正确关闭多线程服务器的套接字 [英] Java: properly closing sockets for multi threaded servers

查看:78
本文介绍了Java:正确关闭多线程服务器的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个多线程服务器,多个客户端可以连接到该服务器并为其提供服务.但是,我不确定在需要时如何适当释放资源.

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.

我的服务器运行一个输入线程(等待用户输入)和一个强制线程(处理连接和用户).我在服务器类中打开一个ServerSocket并将其传递给我的处理线程.看起来像这样:

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. 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);
        }           
    }   
}
}

如果要退出,只需在服务器类中运行此方法以关闭ServerSocket:

If I want to exit, I just run this method in my server class to close the ServerSocket:

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

这将导致下一个serverSocket.accept()调用引发异常,并且由于套接字已关闭,因此循环将停止.

Which should cause the next serverSocket.accept() call to throw an exception, and the loop stops since the socket is closed.

我的问题是-关闭ServerSocket是否会隐式关闭使用它创建的ClientSockets?还是应该确保手动关闭每个打开的ClientSocket?如果是这样,是否有一种简单的方法可以代替将与服务器建立的每个连接都保存在某个地方?

My question is - does closing a ServerSocket implicitly close ClientSockets that were created using it? Or should I make sure to close every single open ClientSocket by hand? If so, is there an easy way to do so instead of saving every single connection made to the server somewhere?

推荐答案

关闭ServerSocket会隐式关闭使用它创建的ClientSocket吗?

does closing a ServerSocket implicitly close ClientSockets that were created using it?

不,对他们没有影响.

还是应该确保手动关闭每个打开的ClientSocket?

Or should I make sure to close every single open ClientSocket by hand?

是的,无论如何,您应该在每个处理程序线程中都这样做.

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.

这篇关于Java:正确关闭多线程服务器的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆