This repository demonstrates three variations of a simple TCP server in Java, each highlighting different concurrency models for handling multiple clients:
- Multithreaded Server (per-client Thread)
- Thread Pool Server (using ExecutorService)
- Single-threaded Server (sequential handling)
- Java 8 or above
- Basic understanding of Java networking (
java.net.Socket
,java.net.ServerSocket
)
Server.java // One of the 3 server implementations at a time Client.java // Simple client to test the servers
javac Server.java Client.java
2. Run the Server
Make sure the server is up and listening before running the client.
#3. Run the Client
java Client You can run the client multiple times (even in a loop) to test concurrency.
✅ 1. Multithreaded Server (Per Client Thread)
Thread thread = new Thread(() -> server.getConsumer().accept(clientSocket)); thread.start(); A new thread is created for each client connection.
Suitable for a small number of concurrent clients.
Simple to implement but not scalable due to thread overhead.
ExecutorService pool = Executors.newFixedThreadPool(100); pool.execute(() -> handleClient(clientSocket)); A fixed number of threads are reused to handle multiple clients.
Efficient and scalable under high load.
Prevents creation of too many threads.
Uses ExecutorService to manage thread lifecycle.
Socket acceptedConnection = socket.accept(); handleClient(acceptedConnection); // directly Handles one client at a time, sequentially.
Simple but not suitable for production or multiple clients.
Useful for debugging or learning socket basics.
Client:
Connects to server via TCP socket.
Sends an initial message (optional).
Reads a welcome message from server.
Server:
Accepts client socket.
Sends a message like "Hello from server ".
Closes the connection.
You can simulate load by modifying the client:
for (int i = 0; i < 100; i++) {
new Thread(client.getRunnable()).start();
}
Try this with:
Multithreaded server (works fine for small N)
Thread pool server (more controlled concurrency)
Single-threaded server (only one handled at a time)
SocketTimeoutException: Server times out before a client connects. (Set a higher timeout or remove it)
Too many open files: Too many simultaneous threads/sockets, especially in multithreaded version.
This repo shows how Java handles socket communication with varying degrees of concurrency and control. The thread pool model is best for most real-world use cases due to its efficiency and scalability.
Created by Amit Kumar
Feel free to fork, modify, and learn!