SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 2 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 22 Dec 2017 My Price 10.00

tested by connecting with an actual webclient

I need help finishing some java code. Attached is the code. Once completed, please answer the following questions with a yes or no.

 

  • program compiles?
  • program is commented adequately?
  • program has been tested extensively?
  • tested by connecting with an actual webclient?import java.io.* ;
    import java.net.* ;
    import java.util.* ;

    final class HttpRequest implements Runnable {
        final static String CRLF = "\r\n";
        Socket socket;
        
        // Constructor
        public HttpRequest(Socket socket) throws Exception {
            // store the socket
            
        }
        
        // Implement the run() method of the Runnable interface.
        public void run() {
        try {
            processRequest();
        } catch (Exception e) {
            System.out.println(e);
        }
        }

        private void processRequest() throws Exception {
        // Get a reference to the socket's input and output streams.
        InputStream is = // get input stream of socket
        OutputStream oss = // get output stream of socket
        DataOutputStream os = new DataOutputStream(oss);
        
        // Set up input stream filters.
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

            // Get the request line of the HTTP request message.
            String requestLine = //

            // Extract the filename from the request line.
            StringTokenizer tokens = new StringTokenizer(requestLine);
               // check that the next token is a get
            // get the file name
            String fileName = tokens.nextToken();
        
            // Prepend a "." so that file request is within the current directory.
            fileName = "." + fileName ;
        
        // Open the requested file.
            FileInputStream fis = null ;
            boolean fileExists = true ;
            try {
            fis = new FileInputStream(fileName);
            } catch (FileNotFoundException e) {
            fileExists = false ;
            }

        // Debug info for private use
        System.out.println("Incoming!!!");
        System.out.println(requestLine);
        String headerLine = null;
        while ((headerLine = br.readLine()).length() != 0) {
            System.out.println(headerLine);
        }
        
        // Construct the response message.
            String statusLine = null;
            String contentTypeLine = null;
            String entityBody = null;
            if (fileExists) {
            statusLine =  // should contain "HTTP/1.0 200 OK"  and a CRLF
            contentTypeLine = "Content-Type: " +
            contentType(fileName) + CRLF;
            } else {
            statusLine = // 404 error msg if not found.
                         // ie. "HTTP/1.0 404 Not Found" + CRLF;
            contentTypeLine = // set it to text/html
                         // ie "Content-Type: text/html" + CRLF;
            entityBody = "<HTML>" +
            "<HEAD><TITLE>Not Found</TITLE></HEAD>" +
            "<BODY>Not Found</BODY></HTML>";
            }
        // Send the status line on os
            os.writeBytes(statusLine);

            // Send the content type line on os
           
            // Send a blank line on os to indicate the end of the header lines.
            

            // Send the entity body.
            if (fileExists) {
            sendBytes(fis, os);
            fis.close();
            } else {
            os.writeBytes(entityBody) ;
            }

            // Close streams and socket.
            os.close();
            br.close();
            socket.close();
        }

        private static void sendBytes(FileInputStream fis,
                      OutputStream os) throws Exception {
        // Construct a 1K buffer to hold bytes on their way to the socket.
        byte[] buffer = new byte[1024];
        int bytes = 0;
        
        // Copy requested file into the socket's output stream.
        while ((bytes = fis.read(buffer)) != -1) {
            os.write(buffer, 0, bytes);
        }
        }

        private static String contentType(String fileName) {
        if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
            return "text/html";
        }
        if(fileName.endsWith(".ram") || fileName.endsWith(".ra")) {
            return "audio/x-pn-realaudio";
        }
        return "application/octet-stream" ;
        }
    }

    public final class WebServer {
        public static void main(String argv[]) throws Exception {
        // Get the port number from the command line.
        int port = (new Integer(argv[0])).intValue();
        
        // Establish the listen socket at port
        ServerSocket socket = //
        
        // Process HTTP service requests in an infinite loop.
        while (true) {
            // Listen for a TCP connection request.
            Socket connection =
            
            // Construct an object to process the HTTP request message.
            HttpRequest request = new HttpRequest(connection);
            
            // Create a new thread to process the request.
            Thread thread = new Thread(request);
            
            // Start the thread.
            thread.start();
        }
        }
    }

Answers

(5)
Status NEW Posted 22 Dec 2017 12:12 PM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)