O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Multiple Clients

while (true) {
											Socket clientSock = socket.accept();
											new Handler(clientSock).start();
											}

To handle multiple clients, we create a thread for each incoming request that we are processing.

In this phrase, we create a new thread to handle the incoming client connection immediately after accepting the connection. This frees our server listener thread to continue listening for other client connections. In the phrase, we are in an infinite while loop so that after a thread is spawned to handle an incoming request, the server immediately goes back to waiting for another request. The Handler class that we use to start the thread from must be a subclass of the Thread class, or it must implement the Runnable interface. The code used ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required