Question: Description: Design a server and client communication system where the project required that your server program a) Creates a log file when the server initially
Description: Design a server and client communication system where the project required that your server program a) Creates a log file when the server initially started. b) Keeps all logs of client connection information with the format of clients IP address, time of connection, file requested by client.
(Use Java programming language)
Server Programming for this
/* This HTTPServer program handles multithreading process.
It is written by BISWAJIT BISWAL.
Please refer to user manual Handout for complte references. */
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
/**
* This class implements a multithreaded simple HTTP
* server that supports the GET request method.
* It listens on port 8080, waits client requests, and
* serves documents.
*/
public class HttpServer {
// The port number which the server
// will be listening on
public static final int HTTP_PORT = 8080;
public ServerSocket getServer() throws Exception {
return new ServerSocket /* This is HTTPCLIENT program, written in Java*/ /* This Program is written by Biswajit Biswal. Please refer to user manual Handout for references. */ import java.io.*; import java.lang.*; import java.net.*; /** * This program connects to a Web server and downloads the specified URL * from it. It uses the HTTP protocol directly. **/ public class HttpClient { public static void main(String[] args) { try { // Check the arguments if (args.length < 2) throw new IllegalArgumentException("Wrong number of arguments"); // Get an output stream to write the URL contents to /* change made by Biswajit, start */ OutputStream to_file; //if (args.length == 5) to_file = new FileOutputStream(args[5]); //else to_file = System.out; InetAddress host = InetAddress.getByName(args[0]); int port = Integer.parseInt(args[1]); String filename = args[3] ; String command = args[2] ; String message; if (command.equals("GET")){ message = "GET /"+filename+" HTTP/1.1 "; } else if(command.equals("PUT")){ message = "PUT "+filename+" HTTP/1.0 "; } else { message = command+" "+filename+" HTTP/1.1 "; } /* change made by Biswajit, End */ // Open a network socket connection to the specified host and port Socket socket = new Socket(host, port); // Get input and output streams for the socket InputStream from_server = socket.getInputStream(); PrintWriter to_server = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); // Send the HTTP GET command to the Web server, specifying the file. // This uses an old and very simple version of the HTTP protocol //to_server.println("GET /index.htm HTTP/1.0 "); to_server.println(message); to_server.flush(); // Send it right now! /* This part is written by Biswajit, Start */ DataOutputStream out = new DataOutputStream(socket.getOutputStream()); if (command.equals("PUT")){ try { File f = new File(filename); DataInputStream in = new DataInputStream(new FileInputStream(f)); int len = (int) f.length(); System.out.println(len); byte[] buf = new byte[len]; in.readFully(buf); in.close(); out.write(buf); out.flush(); } catch (Exception e) { System.err.println(e); } /* This part is written by Biswajit, end*/ } // Now read the server's response, and write it to the file byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = from_server.read(buffer)) != -1) to_file.write(buffer, 0, bytes_read); // When the server closes the connection, we close our stuff out.close(); socket.close(); to_file.close(); } catch (Exception e) { // Report any errors that arise System.err.println(e); System.err.println("Usage: java HttpClient
}
// multi-threading -- create a new connection
// for each request
public void run() {
ServerSocket listen;
try {
listen = getServer();
while(true) {
Socket client = listen.accept();
ProcessConnection cc = new
ProcessConnection(client);
}
} catch(Exception e) {
System.out.println("Exception:"+e.getMessage());
}
}
// main program
public static void main(String argv[]) throws
Exception {
HttpServer httpserver = new HttpServer();
httpserver.run();
}
}
class ProcessConnection extends Thread {
Socket client;
BufferedReader is;
DataOutputStream os;
DataInputStream from_client;
public ProcessConnection(Socket s) { // constructor
client = s;
try {
is = new BufferedReader(new InputStreamReader
(client.getInputStream()));
from_client = new DataInputStream(client.getInputStream());
os = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
}
this.start(); // Thread starts here...this start() will call run()
}
public void run() {
try {
// get a request from Client and parse it.
String request = is.readLine();
System.out.println( "Request: "+request );
StringTokenizer st = new StringTokenizer( request );
String tokn = st.nextToken();// GET request Proceesed here
if ( (st.countTokens() >= 2) && tokn.equals("GET")){
if ( (request = st.nextToken()).startsWith("/") )
request = request.substring( 1 );
if ( request.equals("") )
request = request + "index.htm";
File f = new File(request);
shipDocument(os, f); // PUT request Processed here
} else if ( (st.countTokens() >= 2) && tokn.equals("PUT")){
request = st.nextToken();
System.out.println(request);
File f = new File(request);
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from_client.read(buffer))!= -1){
fos.write(buffer, 0, bytes_read);
fos.flush();
if (bytes_read < 4096){
os.writeBytes( "HTTP/1.0 200 OK File Created. " );
break;
}
}
} else {
os.writeBytes( "400 Bad Request" );
}
client.close();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
/**
* Read the requested file and ships it
* to the browser if found else sends not found.
*/
public static void shipDocument(DataOutputStream out,File f) throws Exception {
try {
DataInputStream in = new
DataInputStream(new FileInputStream(f));
int len = (int) f.length();
byte[] buf = new byte[len];
in.readFully(buf);
in.close();
//out.writeBytes("
out.writeBytes("HTTP/1.1 200 OK ");
out.writeBytes("Content-Length: " +
f.length() +" ");
out.writeBytes("Content-Type:text/html ");
out.write(buf); // write to client
out.flush(); // empty the buffer
} catch (Exception e) {
out.writeBytes("
out.writeBytes("HTTP/1.0 404 Not Found " + e.getMessage() + " ");
out.writeBytes("Content-Type: text/html ");
out.writeBytes("");
out.flush();
} finally {
out.close();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
