CPSC 329 Software Development Fall 2026

CPSC 329 Lab 10: Server Threads

This lab provides practice with threads in a server: serving each client in its own thread, finding and fixing the problems that come from those threads sharing data, and shutting the server down politely.

Objectives

Successful completion of this lab means that you:

Collaboration and AI

Labs are for learning and practice — you may help and be helped by others when it comes to figuring out what to do, but in the end you need to understand and be able to do the things for yourself.

This lab can be done individually or in pairs. If you work together, use pair programming rather than splitting things up and working in parallel.

You may not use AI to figure out what to do or to do the task for you.

Due Date, Deliverables, and Handin

due Fri 11/13 at the start of class

Parts 1-3 are required. Part 4 is strongly encouraged, but not required for a complete handin.

To hand in your work:


Preliminaries

Provided Code and Files

You have been provided with a server which poses riddles (and, as an optional extension, tells knock knock jokes) along with several clients.

Clients connect to the server and send commands, one per line. The server carries out each command and then waits for the next one until the client closes the connection. The commands:

commandwhat happens
riddleThe server sends a riddle, the client sends its answer, the server replies either correct or incorrect, following incorrect with a second line containing the correct answer.
submit riddleThe client sends two more lines (a riddle and its answer). The server adds the riddle to its collection of riddles.
statsThe server replies with one line of statistics: how many clients it has served, how many riddles it has told, and how many riddles it has in its collection.
shutdownShut down the server. (Not implemented yet — that's part 4.)

The files:

Setup

Create an Eclipse project and share it to a local Git repository:

Running Several Programs at Once

You'll often have a server and two or three clients running at the same time, and it is useful to see all of their output at once. This is easier if you instead run the clients in terminal windows:

Tip: you can use the up arrow key in the terminal to step back through previous commands — press Enter to run the command when you get to the one you want. This saves a lot of typing!


The Joke Server

Work through the parts below in order. Places where you need to add code are marked with TODO comments in the provided code, labeled with the specific steps (e.g. 1a, 1b) — except for part 3, where finding the places is part of the task. Answer the questions in writeup.txt as you go.

0. One Client at a Time

1. A Thread for Each Client

The fix is a thread for each client: like the threaded echo server from class, the main thread only accepts connections and each client's conversation is carried out by a task run on its own thread. ClientHandler will be the task — its run method will do what JokeServer's handleClient does now.

2. Counting

The server's ServerStats object is passed to each client handler but nothing updates it so the stats reported when a client leaves (or sends the stats command) are incorrect.

3. Shared State

The server now has several threads running at once: the main thread plus one thread for each connected client. Each thread has its own local variables and each ClientHandler has its own instance variables — but several of those variables may refer to the same object. Whenever variables or objects are shared and something changes them, thread safety is a concern.

Next, test. StressClient starts 20 simulated clients, each with its own thread and its own connection to the server, all running at once. It has two modes: in the default riddles mode, each simulated client asks for 25 riddles, and in submit mode, each simulated client submits 25 riddles as fast as it can. It asks the server for its stats before and after and reports how much each count (clients served, riddles told, and riddles on file) went up compared to how much it should have gone up. Lines marked with * in the output indicate a mismatch.

Now fix the problems you found. A few things to keep in mind:

4. Shutdown

What happens to a client in the middle of a riddle if you stop the server? Try it.

It would be nicer to be able to shut down the server with a command and to give clients a chance to finish what they are doing first. Shutdown should be polite but firm:

ShutdownClient sends the shutdown command. Right now, the server just prints that shutdown isn't implemented yet.

The loop which accepts connections is in JokeServer's main and is run by the main thread, but the shutdown command is read by a client handler thread. So the client handler needs a way to ask the main thread to stop.

Right now the main thread's loop ends, but the server keeps running until every client leaves — and a client which never answers keeps it running forever.

If You Have Time

This part is optional. If you have time, add one or both of the following. Answer the corresponding questions in writeup.txt.