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 ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.