What is a socket, and how can one server handle thousands of clients on port 443?
A socket is one endpoint of a connection: an IP address plus a port plus a protocol. A TCP connection is identified by the four-tuple of source IP, source port, destination IP and destination port, so many clients can share the server's port 443 because each tuple is unique.
Explain it simply
A restaurant has one phone number, but it can talk to hundreds of customers because each call is a separate line. The restaurant's number is the server's port. Each customer's number is what keeps the calls apart.
Example
Client A 203.0.113.10:51000 -> 198.51.100.5:443
Client B 203.0.113.11:51000 -> 198.51.100.5:443
Client A 203.0.113.10:51001 -> 198.51.100.5:443 (second tab)All three go to the same server port, yet the four-tuples differ in at least one value, so the operating system treats them as three connections. A server calls listen() once on port 443, then accept() returns a new socket for every client. The listening socket stays open for new arrivals.
So the real limit is not 65,535 clients per server port. It is memory, file descriptors and CPU. The 65,535 limit applies to connections from one client IP to one server IP and port.
What to say in the interview
"A socket is an IP, port and protocol endpoint. TCP identifies a connection by source IP, source port, destination IP and destination port. The server keeps one listening socket and gets a fresh socket per client, so port 443 is shared and the limit is system resources."
Common mistakes
- Saying a server can serve only 65,535 clients because there are that many ports.