-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
46 lines (39 loc) · 1.48 KB
/
Server.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
38
39
40
41
42
43
44
45
46
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private final ExecutorService threadPool;
public Server(int poolSize) {
this.threadPool = Executors.newFixedThreadPool(poolSize);
}
public void handleClient(Socket clientSocket) {
try (PrintWriter toSocket = new PrintWriter(clientSocket.getOutputStream(), true)) {
toSocket.println("Hello from server " + clientSocket.getInetAddress());
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
int port = 8010;
int poolSize = 10; // Adjust the pool size as needed
Server server = new Server(poolSize);
try {
ServerSocket serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(70000);
System.out.println("Server is listening on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
// Use the thread pool to handle the client
server.threadPool.execute(() -> server.handleClient(clientSocket));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// Shutdown the thread pool when the server exits
server.threadPool.shutdown();
}
}
}