Permalink
Browse files

Add a java implementation

  • Loading branch information...
1 parent eca6fbc commit e472a37756ef7401c288c01c8bddf73caa62a937 @ansman committed May 6, 2012
Showing with 48 additions and 0 deletions.
  1. +48 −0 java/Server.java
View
@@ -0,0 +1,48 @@
+// javac Server.java && java Server [port=8080] [host=127.0.0.1]
+
+import java.net.*;
+import java.io.*;
+
+public class Server {
+ public static void main(String[] argv) throws IOException {
+ String host = "127.0.0.1";
+ short port = 8080;
+
+ if(argv.length >= 2)
+ host = argv[1];
+
+ if(argv.length >= 1)
+ port = Short.parseShort(argv[0]);
+
+ ServerSocket server = null;
+
+ try {
+ server = new ServerSocket(port, 0, InetAddress.getByName(host));
+
+ System.err.println("Server listening on " + host + ":" + port + "\n");
+
+ while(true) {
+ Socket client = server.accept();
+ PrintWriter out = new PrintWriter(client.getOutputStream(), true);
+ BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
+
+ String input;
+
+ while(!(input = in.readLine()).equals(""))
+ System.out.println(input);
+
+ System.out.println("");
+
+ out.write("HTTP/1.1 200 OK");
+ out.close();
+ in.close();
+ client.close();
+ }
+ }
+ finally {
+ System.out.println("Closing");
+ if(server != null)
+ server.close();
+ }
+ }
+}

0 comments on commit e472a37

Please sign in to comment.