Repository navigation
Expand file tree
/
Copy pathWebServer.java
More file actions
39 lines (30 loc) · 1.38 KB
/
Copy pathWebServer.java
File metadata and controls
39 lines (30 loc) · 1.38 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
37
38
39
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WebServer {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java WebServer <port> <public_path>");
return;
}
int port = Integer.parseInt(args[0]);
String publicPath = args[1];
// Thread Pool // https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html
ExecutorService threadPool = Executors.newFixedThreadPool(10); // async handling up to 10 clients
// https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server runs on port: " + port);
System.out.println("Directory: " + publicPath);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New connection from: " + clientSocket.getRemoteSocketAddress());
// Direct request to a new thread
threadPool.execute(new ClientHandler(clientSocket, publicPath));
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
}
}
}