Wednesday, February 22, 2012

Programming Project 1: Multi-threaded Webserver Part 2

WebServer.java


package clavetter;
import java.io.*;
import java.util.*;
import java.net.*;



public class WebServer{

   
    public static void main(String[] args) throws Exception {
   

        int port = 80;
        ServerSocket wS = new ServerSocket(port);
       
       
       
        while(true) {
            Socket cS = wS.accept();
            HttpRequest request = new HttpRequest(cS);
            Thread thread = new Thread(request);
            thread.start();
           
           
        }      
    }
}


Programming Project 1: Multi-threaded Webserver Part 1

HttpRequest.java

package clavetter;
import java.io.*;
import java.net.*;
import java.util.*;



public final class HttpRequest implements Runnable {

    final static String CRLF = "\r\n";
    Socket socket;
   
    //constructor
   
    public HttpRequest(Socket socket) throws Exception
    {
        this.socket = socket;
    }
   
    public void run()
    {
    try{
        processRequest();
    } catch(Exception e) {
        System.out.println(e);
    }
       
       
       
    }
   
    private void processRequest() throws Exception
    {
        InputStream is = socket.getInputStream();
        DataOutputStream os = new DataOutputStream(socket.getOutputStream());
       
        //Setup input stream filters
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //Get the request line of HTTP request message
        String requestLine = br.readLine();
       
        System.out.println();
        System.out.println(requestLine);
       
        // Extract the filename from the request line.
        StringTokenizer tokens = new StringTokenizer(requestLine);
        tokens.nextToken();  // skip over the method, which should be "GET"
        String fileName = tokens.nextToken();
        System.out.println(fileName);
        // Prepend a "." so that file request is within the current directory.
        fileName = "." + fileName;


       
//comment out when done       
        // Get and display the header lines.
        //String headerLine = null;
        //while ((headerLine = br.readLine()).length() != 0) {
            //System.out.println(headerLine);
    //    }
//comment ^^^^^ out when done   
       
        // Open the requested file.
        FileInputStream fis = null;
        boolean fileExists = true;
        try {
            fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            fileExists = false;
        }

        // Construct the response message.
        String statusLine = null;
        String contentTypeLine = null;
        String entityBody = null;
        if (fileExists) {
            statusLine = "HTTP/1.1 200 OK: ";
            contentTypeLine = "Content-Type: " +
                contentType( fileName ) + CRLF;
        } else {
            statusLine = "HTTP/1.1 404 Not Found: ";
            contentTypeLine = "Content-Type: text/html"+ CRLF;
            entityBody = "<HTML>" +
                "<HEAD><TITLE>Not Found</TITLE></HEAD>" +
                "<BODY>Not Found</BODY></HTML>";
        }
        // Send the status line.
        os.writeBytes(statusLine);

        // Send the content type line.
        os.writeBytes(contentTypeLine);

        // Send a blank line to indicate the end of the header lines.
        os.writeBytes(CRLF);

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



       
       
       
   
        DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
        outToClient.writeBytes("HTTP/1.1 200 OK");
        outToClient.writeBytes("Content-Length: 100");
        outToClient.writeBytes("Content-Type: text/html\n\n");
        outToClient.writeBytes("<html><body><h1>My First Heading</h1><p>My first paragraph</p></body></html>");
        outToClient.close();
        os.close();
        br.close();
        socket.close();
       
    }
   
   
   
   

private static String contentType(String fileName)
{
    if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
        return "text/html";
    }
    if(fileName.endsWith(".png")) {
        return "image/png";
    }
    if(fileName.endsWith(".gif")) {
        return "image/gif";
    }
    if(fileName.endsWith(".jpg")|| fileName.endsWith(".jpeg")) {
        return "image/jpeg";
    }
    return "application/octet-stream";
}

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);
   }
}




}

Sunday, February 19, 2012

HW 2

R5: The host uses a series of messages between it and the other host to determine the processes running on the other host.

R.8:  The first class is Reliable Data Transfer, which is only on TCP. The next is throughput, which is on both TCP and UDP. The next is timing, which is on both TCP and UDP. The last one is security, which is on both services.


P1.
A. True.
B. False, as a persistent connection is a TCP connection between the client and a certain server.
C. True. It is called a parallel connection.
D. False. The DATE: header specifies when the request was created and sent by the server.
E. True. The message wouldn't be doing anything if the entity body were to be empty.

P2.
CDUP - Change to Parent Directory
SMNT - Structure Mount
STOU - Store Unique
RMD - Remove Directory
MKD - Make Directory
PWD - Print Directory
SYST - System

P5.
A. Yes. The reply was provided on Tuesday, March 7th 2008.
B. Saturday, December 10th 2005.
C. 3874
D. Yes, the server agreed to a persistent connection.


P11.

A. Yes, the parallel connections do let him load web pages faster because he's getting a larger share of the bandwidth.
B. In this case no, they would all be about equal since they are getting the same amount of bandwidth.

P18.

A. A WHOIS database is a collection of information about registered domain names.
B.  I used MarkMonitor and Network Solutions LLC.
C. I found that for a popular site (facebook) each of the different types are all hosted in the same place, by facebook. For a less popular site, I found they relegated some things like the nameserver to another company.
D. No, Eastern only has one IP address.
E. Eastern's site didn't show up on the ARIN database, but they don't have a range anyway since they just have one IP.
F. By using WHOIS and nslookup an attacker will be able to easily figure out the IP address and locations of servers for certain services, which gives them some of the info they need to make an attack.
G. WHOIS databases should be available because you should always be able to figure out who the owner of a server is, in case they are hosting illegal content, or are doing something else unsavory.

P19.

A.
;; QUESTION SECTION:
;easternct.edu.   IN A

;; ANSWER SECTION:
easternct.edu.  3502 IN A 149.152.32.102

;; AUTHORITY SECTION:
easternct.edu.  172702 IN NS ecsu-dns2.easternct.edu.
easternct.edu.  172702 IN NS ecsu-dns7.easternct.edu.
easternct.edu.  172702 IN NS ecsu-dns6.easternct.edu.
  
B. 
;; QUESTION SECTION:
;amazon.com.   IN A

;; ANSWER SECTION:
amazon.com.  60 IN A 72.21.211.176
amazon.com.  60 IN A 72.21.214.128
amazon.com.  60 IN A 72.21.194.1

;; AUTHORITY SECTION:
amazon.com.  398 IN NS ns4.p31.dynect.net.
amazon.com.  398 IN NS pdns5.ultradns.info.
amazon.com.  398 IN NS pdns4.ultradns.org.
amazon.com.  398 IN NS pdns2.ultradns.net.
amazon.com.  398 IN NS ns1.p31.dynect.net.
amazon.com.  398 IN NS ns3.p31.dynect.net.
amazon.com.  398 IN NS pdns3.ultradns.org.
amazon.com.  398 IN NS ns2.p31.dynect.net.
amazon.com.  398 IN NS pdns6.ultradns.co.uk.
amazon.com.  398 IN NS pdns1.ultradns.net.

;; ADDITIONAL SECTION:
ns1.p31.dynect.net. 48880 IN A 208.78.70.31
ns2.p31.dynect.net. 48880 IN A 204.13.250.31
ns3.p31.dynect.net. 48880 IN A 208.78.71.31
ns4.p31.dynect.net. 48880 IN A 204.13.251.31
pdns1.ultradns.net. 1056 IN A 204.74.108.1
pdns1.ultradns.net. 1056 IN AAAA 2001:502:f3ff::1
pdns2.ultradns.net. 1191 IN A 204.74.109.1
pdns3.ultradns.org. 1056 IN A 199.7.68.1
pdns4.ultradns.org. 1056 IN A 199.7.69.1
 
P22. 
Client-Server:
When N=10 it will take 750 seconds to download.
When N=100 it will take 5000 seconds to download. 
When N=1000 it will take 50000 seconds to download.
P2P:
When N=10 and U = 300 kbps it will take at minimum 750 seconds to download.
When N=10 and U = 700kbps it will take at minimum 214 seconds to download.
When N=10 and U = 2mbps it will take at minimum 75 seconds to download.
When N = 100 and U = 300kbps it will take at minimum 50 seconds to download.
When N=100 and U = 700kbps it will take at minimum 21 seconds to download.
When N=100 and U = 2mbps it will take at minimum 7.5 seconds to download.
When N=1000 and U = 300 kbps it will take at minimum 50 seconds to download.
When N=1000 and U=700kbps it will take at minimum 2.5 seconds to download.
When N=1000 and U=2mbps it will take at minimum .75 seconds to download.
 

Sunday, February 12, 2012

Chapter 1 Homework

P3.

A.  The network can hold 4n simultaneous connections.
B.  It would support 2n simultaneous connections.

P5.

A.  d(prop) = m / s
B.  d(trans) = L / R
C.  d(end-end) = N(d[prop] + d[trans])    (where N is equal to the number of routers)
D.  Ignoring the other delays, the last bit of the packet should just be arriving at the router at time t = d(trans)
E.  The first bit of the packet should be nearing it's destination at time t = d(trans)
F.  At time t = d(trans), the first bit of the packet should just leaving to it's destination
G.  d(prop) and d(trans) will be approximately equal at a distance of 50,000,000 meters.


P18.

A. The average round trip time was 6ms.
B.  There were always six different routers.
C.  There appeared to be only two different ISPs.
D.  The inter-continental connection was much more complex. There were fourteen different routers and three different ISPs on each trial. The average round trip time was 73ms.



P30.

A.  It should take ~200 minutes.
B.  It should take 3 seconds for the first packet to reach its final destination. The second packet should arrive at 4 seconds.
C. It should take ~67 minutes for the final packet to reach the final destination. This is much faster than the non-message switched file.
D. There are a couple downsides to message segmentation. First of all, the packets have to be reassembled at the final destination, taking time. Also, since the header size of packets is usually the same, regardless of size, you'll end up with much more data to transfer with multiple packets.