简体   繁体   English

ServerSocket.accept 给了我多个连接,虽然我只连接一次

[英]ServerSocket.accept gives me multiple connections although I am only connecting once

I have an ExecutorService that creates a new Thread for every connection:我有一个 ExecutorService 为每个连接创建一个新线程:

boolean running;
Socket socket;
ExecutorService executorService= Executors.newCachedThreadPool();
ss=new ServerSocket(port);
while(running){
        socket=ss.accept();
        executorService.submit(new ConnectionThread(socket));
        }

ConnectionThread constructor looks like this, run method is too long to post here: ConnectionThread 构造函数看起来像这样,run 方法太长,无法在此处发布:

Socket socket;
ConnectionThread(Socket socket){
            System.out.println("connectionThread running...");
            this.socket=socket;
        }

when I refresh the page in my browser, the ConnectionThread constructor tells me that multiple threads have been created.当我在浏览器中刷新页面时,ConnectionThread 构造函数告诉我已经创建了多个线程。 Why does that happen?为什么会这样?

发生这种情况是因为浏览器为不同的目的打开了多个 TCP 连接。

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

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