import java.io.*; import java.net.*; public class HTTPServer { public static void main(String[] args) throws Exception { final int httpd = Integer.parseInt(args[0]); // our port ServerSocket ssock = new ServerSocket(httpd); System.out.println("Have opened port " + httpd + " locally."); Socket sock = ssock.accept(); System.out.println("Client has made socket connection."); OneConnection_A client = new OneConnection_A(sock); String s = client.getRequest(); } } class OneConnection { Socket sock; BufferedReader in = null; DataOutputStream out = null; OneConnection(Socket sock) throws Exception { this.sock = sock; in = new BufferedReader (new InputStreamReader (sock.getInputStream())); out = new DataOutputStream(sock.getOutputStream()); } String getRequest() throws Exception { String s = null; while ((s = in.readLine()) != null) { System.out.println("got: " + s); } return s; } } class OneConnection_A extends OneConnection { OneConnection_A (Socket sock) throws Exception { super(sock); } String getRequest() throws Exception { String s = null; while ((s = in.readLine()) != null) { System.out.println("got: " + s); if (s.indexOf("GET") > -1) { out.writeBytes("HTTP-1.0 200 OK\r\n"); s = s.substring(4); int i = s.indexOf(" "); System.out.println("file: " + s.substring(0, i)); return s.substring(0, i); } } return null; } }