1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
/**
* Java program to create a simple HTTP Server.
*/
public class BenBenWebServer extends Thread {
private int _thePort;
private String _ttt = "";
private boolean continueProducing = true;
public BenBenWebServer(int thePort) {
_thePort = thePort;
}
public void ttt(String newTtt) {
_ttt = _ttt + "</br>" + newTtt;
}
public void stopServer() {
continueProducing = false;
}
public void run() {
try {
ServerSocket server = new ServerSocket(_thePort);
System.out.println("Listening for connection on port " + _thePort + " ....");
while (continueProducing) {
try (Socket socket = server.accept()) {
Date today = new Date();
String httpResponse = "";
httpResponse = httpResponse + "HTTP/1.1 200 OK\r\n";
httpResponse = httpResponse + "Content-Type: text/html\r\n";
httpResponse = httpResponse + "\r\n";
httpResponse = httpResponse + "<html>";
httpResponse = httpResponse + today;
httpResponse = httpResponse + _ttt;
httpResponse = httpResponse + "</html>";
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
}
}
}
catch (IOException e) {}
}
} |