import java.io.*; import java.net.*; import java.util.*; import java.text.*; public class WebServer0 { public static void main(String[] args) throws IOException { int listenPort = 4444; ServerSocket serverSocket = null; PrintWriter toSocket = null; BufferedReader fromSocket = null; try { serverSocket = new ServerSocket(listenPort); } catch (IOException e) { System.err.println("!COULD NOT LISTEN ON PORT " + listenPort); System.exit(1); } while (true) { Socket clientSocket = null; try { System.out.println("!LISTENING ON PORT " + listenPort + "... "); clientSocket = serverSocket.accept(); System.out.println("!NEW CONNECTION"); } catch (IOException e) { System.err.println("!ACCEPT FAILED"); System.exit(1); } toSocket = new PrintWriter(clientSocket.getOutputStream(), true); fromSocket = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine = ""; System.out.println("!RECEIVING MESSAGES..."); inputLine = fromSocket.readLine(); System.out.println("SENDING HEADER"); toSocket.println("HTTP/1.1 200 OK"); toSocket.println("Content-type: text/html"); System.out.println(">SENDING EMPTY LINE"); toSocket.println(""); System.out.println(">SENDING HTML"); toSocket.println(""); toSocket.println("MMT260 WebServer0"); toSocket.println(""); toSocket.println("

Hello World!

"); String rightNow = DateFormat.getDateTimeInstance().format(new Date()); toSocket.println("

" + rightNow + "

"); toSocket.println(""); System.out.println("!CLOSING CLIENT SOCKET"); System.out.println(); toSocket.close(); fromSocket.close(); clientSocket.close(); } } }