Suggestions concerning a multi-threaded Web server

A multi-threaded Web server means a server where each HTTP request is handled by a separate thread.

In Java, you may declare a class ServerThread which extends the Thread class. To start the execution of a ServerThread instance, the method start() must be called on it. Then the thread will execute the body of the run() method which must be defined by the programmer. So, in our case, the run() method must look after the reception of an HTTP request and the return of the appropriate answer.

If you look at the given HttpMirror program, you see that the main() method has a loop which is repeated for each HTTP request that arrives. There is one instance of a ServerSocket associated with port 80. Each time the accept() method returns, one obtains an instance of the Socket class which represents the end-point of the Transport connection over which the next HTTP request will be sent. The processing of this request starts with the program line after the comment line "// Get input ...".

Therefore, in order to write a multi-threaded program, it is suggested to include this HTTP request processing code in the run() method of the ServerThread class, and pass the instance of the accepted Socket as a parameter of the ServerThread constructor method. The main() method would then simply create a new instance of the ServerThread class when the accept() method returns in its loop.

Good luck with the programming !!!