Thursday, April 17, 2008

Java based multi threaded webserver

Had nothing to do since last few days so decided to make a small webserver, it has a small configuration file also, here the code that I landed up creating
-----------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class myServer {

public static void main(String args[]) {

String configFileName="config.properties";
String docRoot="";
int portNumber=80;

// ---- Load configuration file. ------
try {
configFileName = args[0];
}
catch(ArrayIndexOutOfBoundsException aioobe){
//Look for default configuration file.
}


try{
Properties properties = new Properties();
properties.load(new FileInputStream(configFileName));
System.out.println("Using configuration file "+configFileName);
portNumber = Integer.parseInt(properties.getProperty("port"));
docRoot = properties.getProperty("documentRoot");
}
catch(IOException ioex){
System.out.println("\nConfiguration file not found.\nPlease specify config file to use\nExample:\n\tjava myServer "+configFileName);
return;
}

// declare a server socket and a client socket for the server
ServerSocket echoServer = null;
Socket clientSocket = null;

// declare an input and an output stream
String line="";
DataInputStream is;
PrintStream os;

// Try to open a server socket on port 9876
try {
echoServer = new ServerSocket(portNumber);
System.out.println("HTTP Server started at port number "+Integer.toString(portNumber)+", Document root as "+docRoot+"\nTo stop server press ^c");
}
catch (IOException e) {
System.out.println(e);
}

while(true){ // Server runs infinitely.

try {

clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());

for (int i=1;i<5 ;i++ ){
line = line.concat(is.readLine());
}
Runnable runnable = new HandleClient(os, line, docRoot);
Thread thread = new Thread(runnable);
thread.start();

line="";

}

catch (IOException e) {
System.out.println(e);
}

}
}
}

class HandleClient implements Runnable{
PrintStream printStream;
String clientData;
String requestFileName;
String docRoot;

public HandleClient(PrintStream in_printStream, String in_clientData, String in_docRoot){
this.printStream = in_printStream;
this.clientData = in_clientData;
this.docRoot = in_docRoot;
//Set filename
this.requestFileName = this.clientData.substring(3, this.clientData.indexOf("HTTP/1.1")).trim().replaceAll("/", "\\\\");
this.requestFileName = this.docRoot.concat(this.requestFileName);
}

public void run(){
try {
//System.out.println("About to serve "+this.requestFileName);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
File file = new File(this.requestFileName);
if (file.exists()){
//System.out.println("File Exists");
}
else{
System.out.println("File does not Exist "+this.requestFileName);
this.printStream.println("Not found.");
this.printStream.close();
return;
}

fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
//this.printStream.println(dis.readLine());
this.printStream.write(dis.read());
}
this.printStream.close();
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
//System.out.println("served "+this.requestFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
this.printStream.println("Not found.");
this.printStream.close();
return;

} catch (IOException e) {
e.printStackTrace();
this.printStream.println("Not found.");
this.printStream.close();
return;
}
}
}
---------------------------------------------------------------------------

--------- Configuration file --------------
#Configuration file
documentRoot=d:\\amit\\docroot
port=9999
errorPage=404.html

No comments: