Activity 9.9 - Using a threaded server

Topic

In this activity we see how the name server from previous activities can be extended to deal with more than one client at once. This is discussed in section 9 of Unit 9. The client code is identical to that used in Activity 9.8, and discussed in section 8.2 of the course text.

Materials

For this activity there are two separate projects, one for the server and one for the client. The instructions for this activity and the Solution document can be accessed from both projects.

You should start from the server project if you have not already done this.

In addition to the Solution document, the server project for this exercise contains three classes, which are complete:

In addition to the Solution document, the client project also contains two classes, which are complete:

Task

You are to run a server program, ThreadedNameServer, which can communicate with several NameClient clients.

Instructions

  1. Open the project Ex9_9Server and run it, to start up the server. You should get a message indicating that the server is starting up. The server is set to listen on port 3000.

  2. First try running a single client as in Activity 9.8. Open the project Ex9_9Client and run it.

    In the appropriate text box of the client GUI, enter a user name from those defined in the NameServer method setUpNameDatabase. Press the Go button to send the user name to the server. You should see a reply from the server in the other text box. You can continue entering a user name and receiving a reply as many times as you like. To quit you click on the Quit button. As in Activity 9.8 this will cause the client to terminate but not the server.

  3. The server continues running even after the client has terminated. Now try running several client programs at the same time. To do this, ensure you have the client set as the main project and click on the Run Main Project button several times. You may find that the several instances of the client window are placed on top of each other on screen - in this case you can drag them to different positions on screen using the mouse. Unlike in Activity 9.8, each client should get a response quite soon after you send a user name to the server. There is no priority order - the server simply responds to each message as it receives it. You must quit each client separately by clicking on the Quit button, as before.

  4. The only way to stop this server is terminating it from NetBeans.