Skip to content

Instantly share code, notes, and snippets.

@bjorn-ali-goransson
Created March 5, 2014 09:38
Show Gist options
  • Save bjorn-ali-goransson/9364146 to your computer and use it in GitHub Desktop.
Save bjorn-ali-goransson/9364146 to your computer and use it in GitHub Desktop.
Web Server, last version (including regression)
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.util.Date;
public class WebServerTest {
public static void main(String[] args) throws Exception{
System.out.println("Starting server...");
final ServerSocket server = new ServerSocket(8202);
Thread thread = new Thread(new Runnable() {
public void run() {
while(true){
new Thread(new Runnable() {
public void run() {
String newLine = "\n";
try{
System.out.println("Waiting for requests..." + newLine + newLine);
DataOutputStream out = new DataOutputStream(server.accept().getOutputStream());
String htmlResponse = doHtmlResponse();
String responseString = "HTTP/1.1 200 OK" + newLine + "Content-Type: text/html" + newLine + newLine + htmlResponse;
System.out.print(responseString);
out.writeBytes(responseString);
out.close();
} catch(Exception e){}
}
}).start();
}
}
});
thread.start();
System.out.println("Press stop to exit");
}
private static String doHtmlResponse() {
return "Hello,<br> world! " + new Date().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment