Sockets & ports
Computer Networks ยท 8 interview questions
A socket is the endpoint of a network connection, and a TCP connection is identified by a four-tuple: source address, source port, destination address, destination port. That tuple is what lets one server port handle many simultaneous clients โ each connection differs in at least the client's port.
A server socket is bound to a port and listens; each accepted connection produces a new socket for that client while the listening socket keeps accepting. The confusion people have โ 'how does port 443 serve thousands of users' โ dissolves once you see that the connection, not the port, is the unit of identity.
Ports are split by convention into well-known (0โ1023, needing privilege), registered (1024โ49151), and ephemeral, which the OS assigns to outgoing connections.
Sockets & ports interview questions
- What uniquely identifies a TCP connection?
- The four-tuple of source IP, source port, destination IP and destination port. Two connections may share three of the four and still be distinct.
- How does one server port handle thousands of simultaneous connections?
- Because connections are identified by the whole four-tuple, not the port. Every client contributes a different source address or port, so each connection is distinct even though all share the server's port.
- Why they ask: A conceptual question that catches people who think of a port as a physical channel.
- What's the difference between the listening socket and an accepted socket?
- The listening socket is bound to the port and only produces new connections. Each accept returns a separate socket representing one client connection, while the listening socket carries on accepting.
- What are the three port ranges?
- Well-known 0โ1023, which typically require privilege to bind; registered 1024โ49151, assigned to specific applications; and dynamic or ephemeral above that, used for outgoing connections.
- What is an ephemeral port?
- A short-lived port the OS assigns to the client side of an outgoing connection, so replies can be routed back. It's released when the connection closes, after any TIME_WAIT period.
- Why does restarting a server sometimes fail with 'address already in use'?
- The previous socket is still in TIME_WAIT, holding the port. SO_REUSEADDR lets a new socket bind despite this, which is why servers set it routinely.
- Why don't high-concurrency servers use one thread per connection?
- Each thread costs memory for its stack and adds scheduling overhead, so tens of thousands don't scale. Event loops with epoll or kqueue let one thread watch many sockets and act only on those that are ready.
- What is the listen backlog?
- The queue of connections that have completed the handshake but not yet been accepted. If the application accepts too slowly the queue fills and further connections are refused or dropped.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ right before you'd lose them.
Start free for 7 days