Sample 08


This sample consists of a client/server windows application for maintaining an inventory of items. It permits any number of shipping clients to be launched. Each communicates with a multi-threaded windows server application. The server maintains an inventory file of items whose quantities are updated as clients request shipments. An automatic receiving thread re-stocks the inventory as needed.

To run the program, you must be familiar with how to run Java sample code using a "test" project. If you already have a test project, copy and paste the source statements of this Sample08.txt file into the source file of your test project. Then, re-build the test project and launch its App class several times. Each time, you will be asked to specify the processing mode - either "Server" or "Client". Proceed as follows:

  1. On the first execution, launch as a "Server". You will be asked to specify the location and name or the item file. If the file you specify does not currently exist, a new file will be created. If you specify an existing file, it will be re-opened. The server window will contain a message log that will indicate that the server is operational.

  2. While the server is running, launch the App class one or more additional times. Each time choose "Client" when asked to specify the processing mode. As each client window appears, it will specify that it is connected to the server. Be sure to restore all application windows from the task bar so you can see them during processing. Use the controls of the client window(s) to request shipments from the item inventory. The server window will show the current inventory value and the message log of client requests..

Obviously, there are many parts to this application. The following is a brief summary of what each application class does:

App - This is the main application class. It defines an application for launching either a windows server or a windows client. It uses an object of the OptionDialog class to permit the user to choose either "Server" or "Client" mode.

OptionDialog - Defines a modal dialog box from which the user may choose either of two buttons.

FrameWindow - Provides a foundation on which a windows application can be built. In addition to providing exit handling features to properly close a window, it contains a static (class) method to permit any window to be centered on the screen.

Item - Encapsulates the data and processing of an inventory item. It uses synchronization to lock the object and prevent simultaneous processing by multiple threads. The Serializable interface is implemented to permit object data to be read or written.

Server - Defines a windows program for the server. It will ask you to specify the name and location of the item file. It then creates a server socket on the local host (your computer) and waits for clients to connect to the socket. Each client is assigned its own thread for handling shipping requests. Other threads are launched to automatically re-stock the inventory and to monitor the current inventory value.

ServerThread - Defines the processing of a server thread associated with a single client. It communicates with the client to process the client's transactions.

Receive - Defines a thread to simulate a warehouse receiving process to add items to the inventory at random time intervals.

Value - Defines a thread to display the current inventory value each second.

Shipping - Defines a windows client for specifying how many of an item are to be shipped from the item inventory. It connects to and communicates with a multi-threaded server that assigns it a unique thread to process its transactions.