Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a simple server program in java based on the instructions below. Here is a

ID: 3742342 • Letter: W

Question

Write a simple server program in java based on the instructions below.
Here is also a link to a client program if needed: https://file.io/jLSjYD

Objective: Write a Server Program. This program will simulate a web server such as Apache or IIS using port 18888. The Server Program: The program will wait for a request for a webpage. The client will send two lines (strings) to request the webpage. The first line will be a GET request and the second line will be a blank line. For example: the first line may look like the following: GET /mypage.html HTTP/1.0 The server program will parse the string and in this case will read the "mypage.html" file from the hard drive and send the contents of the file to the client program. After this is completed the server program will continue to wait for another request from the client. (ie the server program will run forever) The file name may vary. Therefore do not assume the file name is always "mypage html" You may assume the file is always in the same subdirectory as the java file. I will use my own client program to grade your work. Please feel free to ask question in order that you will perform the required tasks.

Explanation / Answer

/*

Here's a short program that does it

*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.StringTokenizer;

import java.util.Date;

import java.net.ServerSocket;

import java.net.Socket;

public class Server {

protected ServerSocket serverSocket;

protected int port;

protected boolean ServerOn = true;

public Server() {

this(8888);

}

public Server(int port) {

this.port = port;

try {

serverSocket = new ServerSocket(this.port);

} catch(IOException ioe) {

System.out.println("Could not create socket. Quitting.");

System.exit(-1);

}

System.out.println("Server running on "+ this.port);

  

while(ServerOn) {

try {

Socket client = serverSocket.accept();

ClientThread clientThread = new ClientThread(client);

clientThread.start();

} catch(IOException ioe) {

System.out.println("Exception found on accept. Ignoring. Stack Trace :");

ioe.printStackTrace();

}  

}

try {

serverSocket.close();

System.out.println("Server Stopped");

} catch(Exception ioe) {

System.out.println("Error stopping server socket");

System.exit(-1);

}

}

public static void main (String[] args) {

new Server(18888);   

}

class ClientThread extends Thread {

protected Socket clientSocket;

protected boolean runThread = true;

public ClientThread() {

super();

}

ClientThread(Socket s) {

clientSocket = s;

}

public void run() {

BufferedReader in = null;

PrintWriter out = null;

System.out.println(

"Accepted Client - " + clientSocket.getInetAddress().getHostName());

try {

in = new BufferedReader(

new InputStreamReader(clientSocket.getInputStream()));

out = new PrintWriter(

new OutputStreamWriter(clientSocket.getOutputStream()));

  

while(runThread) {

String clientCommand = in.readLine();

if(clientCommand == null) {

runThread = false;

break;

}

System.out.println("Client Says :" + clientCommand);

StringTokenizer parse = new StringTokenizer(clientCommand);

String method = parse.nextToken().toUpperCase();

String file = parse.nextToken();

if (method.equals("GET")) {

File f = new File("." + file);

sendFile(out, f);

runThread = false;

}

}

} catch(FileNotFoundException e) {

System.out.println("File Not Found");

fileNotFound(out);

} catch(Exception e) {

e.printStackTrace();

}

finally {

try {

in.close();

out.close();

clientSocket.close();

System.out.println("...Stopped");

} catch(IOException e) {

e.printStackTrace();

}

}

}

private void fileNotFound(PrintWriter out) {

out.println("HTTP/1.1 404 File Not Found");

out.println("Server: Java HTTP Server");

out.println("Date: " + new Date());

out.println("Content-Type: text/html");

out.println("Connection: Closed");

out.println("Content-length: 25");

out.println(); // blank line between headers and content, very important

out.println("<html>404 Not Found</html>");

out.flush();

}

private void sendFile(PrintWriter out, File f) throws IOException, FileNotFoundException {

BufferedReader br = new BufferedReader(new FileReader(f));

out.println("HTTP/1.1 200 OK");

out.println("Server: Java HTTP Server");

out.println("Date: " + new Date());

out.println("Content-Type: text/html");

out.println("Connection: Closed");

out.println("Content-length: " + f.length());

out.println(); // blank line between headers and content, very important

String st;

while ((st = br.readLine()) != null) {

out.println(st);

}

out.flush();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote