by Daniel J. Tanner
March 9th, 2010
CMPT 352
This program implements some basic features of the HTTP protocol
**/
import java.io.*;
import java.net.*;
import java.util.*;
public class TannerWebServer
{
//constants & globals
public static final int PORT = 2880;
public static Configuration configurator = null;
public static WeakHashMap
public static File logFile = null;
public static void main(String[] args) throws java.io.IOException, ConfigurationException
{
//begin by initializng the configurator
try
{
configurator = new Configuration("../conf/config.xml");
}
catch (ConfigurationException ce)
{
System.out.println(ce);
System.exit(0);
}
//load the initial files into a hashmap
cache = new WeakHashMap
cache.put("/index.html", new File(configurator.getDefaultDocument()));
cache.put("/400.html", new File(configurator.get400()));
cache.put("/404.html", new File(configurator.get404()));
//load logfile
logFile = new File(configurator.getLogFile());
//Create socket
ServerSocket sock = null;
try
{
//establish socket
sock = new ServerSocket(PORT);
while(true)
{
Socket client = sock.accept();
//debug testing
InetAddress ipAddr = client.getInetAddress();
System.out.print(ipAddr.getHostAddress() + " : ");
System.out.println(client.getPort());
Thread worker = new Thread(new TannConnection(client));
worker.start();
}
}
//catch any errors opening sockets
catch(IOException ioe)
{
System.err.println(ioe);
System.exit(0);
}
finally
{
//close streams
if(sock != null)
sock.close();
}
}
}
No comments:
Post a Comment