cs654/lab3 partA finished

git-svn-id: svn://anubis/gvsu@4 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-02-03 02:47:51 +00:00
parent 7a22188417
commit a82373c0fe
4 changed files with 83 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,53 @@
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 {
this.socket = 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 {
// Set up output stream
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
// Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Get the request line of the HTTP request message.
String requestLine = br.readLine();
// Display the request line.
System.out.println();
System.out.println(requestLine);
// Get and display the header lines.
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0) {
System.out.println(headerLine);
}
// Close streams and socket.
os.close();
br.close();
socket.close();
}
}

Binary file not shown.

View File

@ -0,0 +1,30 @@
import java.io.* ;
import java.net.* ;
import java.util.* ;
public final class WebServer {
public static void main(String argv[]) throws Exception {
int port = 8080;
// Establish the listen socket.
ServerSocket socket = new ServerSocket(port);
// Process HTTP service requests in an infinite loop.
while (true) {
// Listen for a TCP connection request.
Socket connection = socket.accept();
// 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();
}
}
}