package webserver;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class WebServer {
static ServerSocket requestListener;
static Socket requestHandler;
static Scanner requestReader;
static String HTTPMessage;
static String requestedFile;
public static int HTTP_PORT = 12346;
public static void main(String[] args) {
try{
requestListener = new ServerSocket(HTTP_PORT);
System.out.println("Waiting For IE to request a page");
requestHandler = requestListener.accept();
System.out.println("Page Requested: Request Header");
requestReader = new Scanner(new InputStreamReader(requestHandler.getInputStream()) );
int lineCount = 0;
do{
lineCount++; //This will be used later
HTTPMessage = requestReader.nextLine();
System.out.println(HTTPMessage);
HTTPMessage = requestReader.next();
if(lineCount==1){
requestedFile="WebRoot\\" + HTTPMessage.substring(5,HTTPMessage.indexOf("HTTP/1.1")-1);
requestedFile = requestedFile.trim();
pageReader = new Scanner(new File(requestedFile));
pageWriter = new DataOutputStream(requestHandler.getOutputStream()) ;
while(pageReader.hasNext()){
String s = pageReader.nextLine();
pageWriter.writeBytes(s);
pageReader.close();
pageWriter.close();
requestHandler.close();
}
}
}while(HTTPMessage.length() != 0);
}catch(Exception e){
System.out.println(e.toString());
System.out.println("\n");
e.printStackTrace();
}
}
}
--- Update ---
Kindly help look at the reason why this code is not running.
The browser was showing waiting for the request page, but would eventually not find it
Thanks