TCPServer
is a simple TCP server implemented in Java. It accepts client connections and responds with a basic "Hello World" message using the HTTP protocol.
- Utilizes a fixed thread pool to handle multiple client connections concurrently.
- Uses the standard Java logging framework for logging events and errors.
- Implements resource management to ensure proper closure of sockets and streams.
-
Compile the
TCPServer.java
file:javac TCPServer.java
-
Run the compiled Java class:
java TCPServer
-
The server starts and listens on port
8080
. You can connect to it using a web browser or tools likecurl
:curl http://localhost:8080
- PORT: The port on which the server listens. Currently set to
8080
. - THREAD_POOL: A fixed-size thread pool executor used for handling client connections. The size is currently set to
5
.
The main method initializes the server socket and starts listening for client connections. For each client connection, it dispatches the processing task to the thread pool.
Handles the client connection. It writes a basic HTTP response with the "Hello World" message to the connected client.
-
Parameters:
clientSocket
: The socket representing the client connection.
-
Behavior:
- Writes an HTTP response with "Hello World" content.
Errors and exceptions are logged using the standard Java logging framework. Specific catch blocks are provided for IOException
and general exceptions to ensure meaningful error messages.
- Java Standard Library