In this discussion, we continue the discussion of the Java client/server architecture begun in the earlier tutorial. Specifically, we will look at a multi-threaded server and client.The relevant classes are contained in the
java.lang
package. In particular, look at theThread
class and theRunnable
interface.If you wish to run the applications discussed here, you may do so using the code included in the programs directory stored with this tutorial.
Multi-Threaded Client and Server
The discussions that follow extend the client and server programs discussed in the Java Clients and Servers I tutorial.The first goal is to make the client multi-threaded so that it runs the user interface with one thread and handles the I/O with the server in a separate thread. A second goal is to make the server multi-threaded so that it handles a user interface with one thread and the I/O with multiple clients with multiple additional threads. We will use a single additional thread to listen on a "well-known" port for clients. After each client connects, the server will create a separate thread to handle each client.
The discussion for each program will include two parts: first, the basic thread logic and, second, the complete code.
Client
Server
References
References useful for this discussion include the following:
- Sun's Java Development Kit (JDK)
- Sun's Java API
- Java API Class Hierarchy
- Source Code for JDK Packages