- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHTTPServer.java
More file actions
Latest commit
36 lines (31 loc) · 1.45 KB
/
Copy pathSimpleHTTPServer.java
File metadata and controls
36 lines (31 loc) · 1.45 KB
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
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.sql.Date;
publicclassSimpleHTTPServer {
publicstaticvoidmain(String[] args) throwsException {
// Create a server object that listens for incoming connections on port 8080.
finalServerSocketserver = newServerSocket(8080);
System.out.println("Listening for connection on port 8080...");
// Infinite loop because we don't want the server to open and close.
while (true) {
// Create a Socket object which represents the connection and is used to read incoming HTTP request and send HTTP response.
try(SocketclientSocket = server.accept()) {
// Read the incoming HTTP request
InputStreamReaderisr = newInputStreamReader(clientSocket.getInputStream());
BufferedReaderreader = newBufferedReader(isr);
// Read the first line of the request
Stringline = reader.readLine();
// Print all the lines of the request
while (!line.isEmpty()) {
System.out.println(line);
line = reader.readLine();
}
Datetoday = newDate(0);
StringhttpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
clientSocket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
}
}
}
}