📚 Table of Contents
- 🖥️ What Is a Server?
- 🔄 What Is Multi-Threading?
- ❓ Why Do We Need a Multi-threaded Server?
- 🛠️ How to Design Multi-Threaded Servers?
🖥️ What Is a Server?
It is the process that listens to the TCP protocol on some port, addresses the client request, and sends the response.
🔄 What Is Multi-Threading?
Multithreading is a programming concept that allows multiple threads of execution to run concurrently within a single process.
❓ Why Do We Need a Multi-threaded Server?
Let's imagine we have a single-threaded server that will process one request at a time. Now, imagine what if millions of requests come in.
I've created a server that processes requests one after the other. Each new request waits until the previous one is completed. Take a look at the response times in different terminals:
Now, consider a multithreaded server architecture. It can handle multiple client connections simultaneously using threads. Each incoming connection gets its own thread, enabling the server to serve multiple clients at once.
In our example, all four clients receive their responses concurrently after four seconds. This demonstrates the efficiency of a multithreaded server in handling multiple requests simultaneously.
🛠️ How to Design Multi-Threaded Servers?
Step 1: Open the Socket to Listen to the Port
Create theServerSocket
object that will accept the TCP connection from port1234
.
Step 2: Accept the Client's Connection to That Port
Accept the client request using theServerSocket.accept()
.
It is a blocking system call meaning it will not move further without accepting the connection.
ServerSocket.accept()
will return theSocket
object specific to that client connection, which will be used for reading requests and sending responses to that client.
Disclaimer: 🚨 This article aims to explain multithreaded servers. However, there are practical limitations to this design. Creating millions of threads to handle millions of requests can strain system resources. These issues can be addressed by using strategies such as thread pooling.
🌟 Thank you for reading! I appreciate your time and hope you found the article helpful. 📚 I'm open to suggestions and feedback, so feel free to reach out. Let's keep exploring together! 🚀
Top comments (2)
Good Job @mjsf1234
Well written and with code examples