import java.io.*; import java.net.ServerSocket; import java.net.Socket; class WebServer { public static final int PORT = 80; ServerSocket serversock; WebServer() throws IOException { serversock = new ServerSocket(PORT); } public void run() { int services = 0; System.out.println("I am started"); while(services++ < 3) { try { Socket sock = serversock.accept(); BufferedReader br = new BufferedReader( new InputStreamReader( sock.getInputStream()) ); String res = br.readLine(); System.out.println( res ); PrintStream out = new PrintStream( new BufferedOutputStream ( sock.getOutputStream() )); composeAnswer("Hello: " + res, out); br.close(); out.close(); } catch(IOException e) {System.err.println(e); } } } private void composeAnswer(String s, PrintStream out){ String statusLine = "HTTP/1.0 200 OK"; String serverLine = "Server: Our simple http server"; String contentTypeLine = "text/html"; String entityBody = "" + "Hello there" + "" + s + "" ; try{ String contentLengthLine = "Content-Length: " + entityBody.length(); out.println(serverLine); out.println(contentTypeLine); out.println(contentLengthLine); out.println(); out.print(entityBody); out.flush(); }catch(Exception e){System.out.println("Error: "+ e);} } public static void main(String[] args) throws IOException { WebServer server = new WebServer(); server.run(); } }