MERHABA;
arkadaşlar staj dosyam için patron bana bu java web server projesini verdi ama ne işe yaradıgını bilmiyorum staj dosyama da acıklama yazmam gerekiyo yardımcı olurmusunuz?
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class HttpServer {
public static final int HTTP_PORT = 8080;
public ServerSocket createSocket() throws Exception {
return new ServerSocket(HTTP_PORT);
}
public void run() {
ServerSocket listen;
try {
listen = createSocket();
while(true) {
Socket client = listen.accept();
ConnectionHandler handler = new
ConnectionHandler(client);
}
} catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
public static void main(String argv[]) throws
Exception {
HttpServer httpserver = new HttpServer();
httpserver.run();
}
}
class ConnectionHandler extends Thread {
Socket client;
BufferedReader is;
DataOutputStream os;
public ConnectionHandler(Socket s) { // constructor
client = s;
try {
is = new BufferedReader(new InputStreamReader
(client.getInputStream()));
os = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
System.out.println("Exception: "+e.getMessage());
}
this.start();
}
public void run() {
try {
String request = is.readLine();
System.out.println( "Request: "+request );
StringTokenizer st = new StringTokenizer( request );
if ( (st.countTokens() >= 2) &&
st.nextToken().equals("GET") ) {
if ( (request =
st.nextToken()).startsWith("/") )
request = request.substring( 1 );
if ( request.equals("") )
request = request + "index.html";
File f = new File(request);
sendDocument(os, f);
} else {
os.writeBytes( "400 Bad Request" );
}
client.close();
} catch (Exception e) {
System.out.println("Exception: " +
e.getMessage());
}
}
public static void sendDocument(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("HTTP/1.0 200 OK\r\n");
out.writeBytes("Content-Length: " +
f.length() +"\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.write(buf);
out.flush();
} catch (Exception e) {
out.writeBytes("\r\n\r\n");
out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("");
out.flush();
} finally {
out.close();
}