Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (46 loc) · 1.72 KB

File metadata and controls

53 lines (46 loc) · 1.72 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
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 (Socket socket=clientSocket;
PrintWriter toSocket = new PrintWriter(clientSocket.getOutputStream(), true)) {
toSocket.println("Hello from server " + clientSocket.getInetAddress());
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
int port = 8010;
int poolSize = 100; // 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();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.