首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSLServerSocket在线程中接受SSLSocket客户端,有最大限制吗?

SSLServerSocket在线程中接受SSLSocket客户端,有最大限制吗?
EN

Stack Overflow用户
提问于 2013-06-03 17:56:29
回答 1查看 2.7K关注 0票数 0

我在Java中有一个SSLServerSocket,当客户端连接时,我为它的通信创建一个线程:

代码语言:javascript
复制
    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();
        }
    }

问题是代码有时会拒绝连接。它发生在代码运行一段时间时,所以我认为问题是客户端失去了连接和重连接,但前一个线程仍然活着,并且存在最大的SSLServerSockets。

这会发生吗?最大值是多少?

当连接断开时,我如何杀死线程?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-03 18:34:56

根据您的代码和我对网络的理解(从较低级别和API级别),您可能错误地使用了API。

在较高的级别上,您希望以不同的方式执行此操作

代码语言:javascript
复制
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();
        }
    }
}

我希望它能更正确地回答您的问题。

关于EJP的评论-我已经更新了我的解释,并引用了位于here的文档

将传入连接指示(连接请求)的最大队列长度设置为backlog参数。如果连接指示在队列已满时到达,则连接将被拒绝。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16894125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档