826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package udptcp;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class TCPPortCServer {
    private static final int PORT = 8080; // HTTPĬÈ϶˿Ú
    private static final int MAX_CONNECTIONS = 20000;
    private static final ExecutorService executor = Executors.newFixedThreadPool(100);
 
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("HTTP Server started on port " + PORT);
            
            while (true) {
                Socket clientSocket = serverSocket.accept();
                executor.execute(new HTTPHandler(clientSocket));
            }
        } catch (Exception e) {
            System.err.println("HTTP Server crashed: " + e.getMessage());
        }
    }
 
    static class HTTPHandler implements Runnable {
        private final Socket socket;
 
        HTTPHandler(Socket socket) {
            this.socket = socket;
        }
 
        @Override
        public void run() {
            try {
                // ¼ò»¯µÄHTTPÇëÇó´¦Àí
                byte[] response = "HTTP/1.1 200 OK\r\n\r\nHello from TCP-C".getBytes();
                socket.getOutputStream().write(response);
                socket.close();
            } catch (Exception e) {
                System.err.println("HTTP handling error: " + e.getMessage());
            }
        }
    }
}