Handling multiple clients can be achieved using threads. In this section, we will develop a simple parts server and client applications. The server will use a separate thread to handle each client. This technique is simple to implement, but it will not always be suitable for more demanding applications. We will introduce alternate techniques to multitask in Chapter 7, Network Scalability.
The parts server is implemented in the PartsServer
class, and the client is implemented in the PartsClient
class. A new instance of a ClientHandler
class will be created for each client. This handler will accept requests for the price of a part. The client will send the name of the part to the handler. The handler will look up the price of the part using the getPrice
method of PartsServer
. It will then return the price to the client.