skulpt
skulpt

Reputation: 527

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. 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:

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.

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?

Upvotes: 4

Views: 4095

Answers (2)

Shady Atef
Shady Atef

Reputation: 2401

Let the client handler thread, closes the client socket on the end of processing.

Upvotes: 0

user207421
user207421

Reputation: 310860

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

No, it has no effect on them.

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.

Upvotes: 1

Related Questions