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
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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.