Thread-per-connection approach

In this approach, a single thread is used to handle all of the client's requests. This approach will require that the client send some sort of notification that it has no further requests. In lieu of an explicit notification, a timeout may need to be set to automatically disconnect the client after a sufficient period of time has elapsed.

The thread-per-connection server

Modify the server's run method by commenting out the bulk of the try block where the request is handled and the response is sent to the client. Replace it with the following code. In the infinite loop, the command request is read. If the request is quit, then the loop is exited. Otherwise, the request is handled in the same way as before:

 while(true) ...

Get Learning Network Programming with Java 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.