Building a Multithreaded Web Server with Sockets


Your last task is to combine all the socket topics covered previously. Going from the previous code listings in this chapter to a full Web server is not difficult. There are many details you could add, but you need only two more fundamental components to create a Web server from the socket functionality demonstrated previously in this chapter: the protocol and the file service. The protocol is simply an agreed-on rule about what the client should send. For this example, the client must send the word "zakoog" and then a filename. The server will then know to grab a file and return it. To add this Web server mainstay, change the Wife class as shown in Listing 11.11, which now returns an HTML file as opposed to just a text message (as was the case in Listing 11.7).

Listing 11.11 Making a Multithreaded Web Server
 class Wife {    public boolean turnOff = false;    private String answer = "";    private String[] sweetNothings =      { "Take out that smelly garbage.",        "Mow the lawn you bum.",        "Fix the sink you broke.",        "We talked about this already.",        "No! My birthday was last Monday."      };    public String answer(String husbandSays) {         String theOutput = null;         if (husbandSays == null)         {             this.answer = "I love you, dear.";         } else if (husbandSays.indexOf("yes dear")!=-1)         {            this.answer = "I still love you, honey.";            turnOff = true;         //this is the command word that tells         //this server to return a file rather than         //simply a text message. With this capability.         //you have created a file server over sockets,         //the core functionality of a Web server.         } else if (husbandSays.indexOf("zakoog")!=-1)         {            StringBuffer fileContents = new StringBuffer();            try            {               int pos = husbandSays.indexOf("zakoog");               String fileLine = "", fileName = "";               fileName = husbandSays.substring(pos + 7);               FileReader fileReader = new FileReader(fileName);               BufferedReader bufferedReader =                                         new BufferedReader(fileReader);               //fileContents.append(bufferedReader.readLine());               while ((fileLine = bufferedReader.readLine()) != null)               {                  fileContents.append(fileLine);               }            } catch (IOException e)            {               this.answer = "404 FILE NOT FOUND";            }            this.answer = fileContents.toString();         } else         {            Random random = new Random();            int whisper = random.nextInt(5);            this.answer = this.sweetNothings[whisper];         }         return this.answer;     } } 

In this listing, you added just a few lines to the previous Wife class to create your multithreaded server. To test it, stop the client and server, recompile the WifeThread file, and restart the server ( java WifeMultithreadedServer ) and the client ( java HusbandClient ). Your refreshed environment is then close to a "real" browser and Web server. The protocol isn't HTTP, so technically you haven't built a Web server and browser. However, the principle is much the same. Type zakoog files/mytext.txt in the client, and the text of that file should be returned to the client.

The real Web requires that you start a request with the protocol (such as http) , but your custom application uses zakoog , which does the same thing. Both keywords represent part of a protocol ” http for Hypertext Transfer Protocol and zakoog for Wife Server protocol. Tim Berners-Lee created HTML, HTTP, and the precursor of the URL. There are many problems with these protocols, and Berners-Lee is quick to complain about them. For example, they are asynchronous, meaning the Web is basically a request-and-response model. Perhaps you can build a better protocol, such as the Wife Server protocol.

Although your certification project doesn't need all the functionality demonstrated in this chapter, you do have enough examples to select from to build the socket portion of the assignment.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net