Search code examples
javamultithreadingserversocket

SSLServerSocket accepts SSLSocket client in thread, is there a maximum?


I have a SSLServerSocket in Java, when a client is connected, I create a thread for its communication:

    System.setProperty("javax.net.ssl.keyStore", "keystore");
    System.setProperty("javax.net.ssl.keyStorePassword", "password");

    SSLServerSocket server = (SSLServerSocket)null;

    if(ipSocket == null){
        ipSocket = new HashMap<String,java.net.Socket>();
    }

    try {

        SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        server = (SSLServerSocket) sslserversocketfactory.createServerSocket(4380);
        log.info("Server started");

    } catch(IOException e) {
        e.printStackTrace();
    }

    while(true){

        try {
            SSLSocket client = (SSLSocket) server.accept();
            log.info("new client");

        } catch (Exception e){
            e.printStackTrace();
        }
    }

The problem is when the code sometimes rejects connections. It happens when the code is running for a while, so I think the problem is the clients lost the connection and reconect, but the previous thread is still alive, and there is a maximun SSLServerSockets.

Could this happen? What number is the maximum?

How can I kill the threads when a disconnection happens?


Solution

  • Based on your code and my understanding of networking (both from the lower level and from the API level) you may be incorrectly using the API.

    At a high level, you want to do this a little differently

    public static void main(String[] args) {
        System.setProperty("javax.net.ssl.keyStore", "keystore");
        System.setProperty("javax.net.ssl.keyStorePassword", "password");
        SSLServerSocket server = (SSLServerSocket)null;
        if(ipSocket == null){
            ipSocket = new HashMap<String,java.net.Socket>();
        }
    
        try {
            SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            // Creates a socket with a default backlog of 50 - meaning
            // There will be a maximum of 50 client connection attempts on this 
            // socket after-which connections will be refused. If the server is
            // overwhelmed by more than that number of requests before they can be
            // accepted, they will be refused
            // The API allows for you to speccify a backlog.
            server = (SSLServerSocket) sslserversocketfactory.createServerSocket(4380);
            log.info("Server started");
        } catch(IOException e) {
            e.printStackTrace();
        }
    
        while(true){
            try {
                // This will take one of the waiting connections
                SSLSocket client = (SSLSocket) server.accept();
                log.info("new client");
                // HERE is where you should create a thread to execute the
                // conversation with the client.
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    I hope that more correctly answers your question.

    In regards to the comment by EJP - I have updated my explanation and cited the documentation located here:

    The maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter. If a connection indication arrives when the queue is full, the connection is refused.