Networking Java Solutions Homework Unfortunately this is not present in every response

subject Type Homework Help
subject Pages 7
subject Words 1137
subject Authors James F. Kurose, Keith W. Ross

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Solution for the Proxy Cache Lab
The following is one solution for the proxy cache lab. It contains code for all the three
classes in the lab.
ProxyCache.java
/**
* ProxyCache.java - Simple caching proxy
*
public static void init(int p) {
port = p;
try {
socket = new ServerSocket(port);
} catch (IOException e) {
try {
BufferedReader fromClient =
new BufferedReader(new
InputStreamReader(client.getInputStream()));
System.out.println("Reading request...");
page-pf2
new DataOutputStream(server.getOutputStream());
toServer.writeBytes(request.toString());
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + request.getHost());
System.out.println(e);
//response = new HttpResponse(is);
DataOutputStream toClient =
new DataOutputStream(client.getOutputStream());
toClient.writeBytes(response.toString());
toClient.write(response.body);
myPort = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Need port number as argument");
System.exit(-1);
} catch (NumberFormatException e) {
page-pf3
handle(client);
//ProxyCacheThread request = new ProxyCacheThread(client);
//request.run();
} catch (IOException e) {
System.out.println("Error reading request from client: " + e);
/* Definitely cannot continue, so skip to next
* iteration of while loop. */
continue;
}
}
}
}
HttpRequest.java
/**
* HttpRequest - HTTP request container and parser
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpRequest {
/** Help variables */
final static String CRLF = "\r\n";
final static int HTTP_PORT = 80;
/** Store the request parameters */
String method;
firstLine = from.readLine();
} catch (IOException e) {
System.out.println("Error reading request line: " + e);
}
page-pf4
String[] tmp = firstLine.split(" ");
method = tmp[0];
* contact in case the request URI is not complete. */
if (line.startsWith("Host:")) {
tmp = line.split(" ");
if (tmp[1].indexOf(':') > 0) {
String[] tmp2 = tmp[1].split(":");
port);
}
/** Return host for which this request is intended */
public String getHost() {
return host;
}
/** Return port for server */
page-pf5
return req;
}
}
HttpResponse.java
/**
* HttpResponse - Handle HTTP replies
*
* $Id: HttpResponse.java,v 1.3 2004/02/16 15:21:46 kangasha Exp $
*
*/
import java.io.*;
String version;
int status;
String statusLine = "";
String headers = "";
/* Body of reply */
/* First read status line and response headers */
try {
String line = fromServer.readLine();
while (line.length() != 0) {
if (!gotStatusLine) {
page-pf6
}
length = Integer.parseInt(tmp[1]);
}
line = fromServer.readLine();
}
} catch (IOException e) {
System.out.println("Error reading headers from server: " + e);
return;
}
* than BUF_SIZE. The while-loop ends when either we have
* read Content-Length bytes or when the connection is
* closed (when there is no Connection-Length in the
* response. */
while (bytesRead < length || loop) {
body[bytesRead + i] = buf[i];
}
bytesRead += res;
}
} catch (IOException e) {
System.out.println("Error reading response body: " + e);
return;
}
}
page-pf7
/**
}
}

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.