-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPServer.java
37 lines (30 loc) · 1.11 KB
/
HTTPServer.java
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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class HTTPServer {
private int port;
private FileServer fileServer;
public HTTPServer(int port, String rootDirectory) {
this.port = port;
this.fileServer = new FileServer(rootDirectory);
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server listening on port " + port + "...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
Thread thread = new Thread(new RequestHandler(clientSocket, fileServer));
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port = 1202;
String rootDirectory = "/path/to/your/static/files/directory";
HTTPServer server = new HTTPServer(port, rootDirectory);
server.start();
}
}