Search code examples
javasocketsexecutorserviceserversocket

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


I have an ExecutorService that creates a new Thread for every connection:

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:

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. Why does that happen?


Solution

  • This happens, because the browser opens several TCP connections for different purposes.