The main class in a quiz server is the QuizServer class. It's instantiated every time you run a quiz, and holds all the user interface code. The QuizServer class also implements the Runnable interface, which enables instances of QuizServer to be executed by a thread. By creating a new thread for each request received, servers can handle many requests simultaneously. The Runnable interface lets you add thread support to any class and requires only that you declare a run() method with no arguments inside your class.

The QuizServer() constructor creates a new Thread object by passing a reference to itself in the thread's constructor:

serverThread = new Thread(this, "serverThread");

Next, the thread's start() method is called, which executes the QuizServer run() method. The run() method creates a new instance of a ServerSocket class. Each ServerSocket instance is bound to a specific port address and can only listen on that address. The port address is specified when calling its ServerSocket() constructor. The ServerSocket constructor calls the accept() method in a loop looking for a request, and the method won't return until it receives one. When a request is received, a new ServerRequest instance is created to handle the request. The ServerRequest class extends the Thread class so the Thread class can execute in its own thread. This is another way to create a threadable class.