TCP vs UDP, the Three-Way Handshake, and How the Kernel Maps Packets to Sockets
Company: Canonical
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Two related questions about TCP and sockets: how TCP differs from UDP and how a TCP connection is opened, and how sockets relate to TCP, including how the kernel decides which connected socket an incoming TCP packet belongs to.
### Clarifying Questions
- Should the demultiplexing answer describe Linux specifically, or the general mechanism?
- Should IPv6 be covered as well as IPv4?
### Part 1 — TCP versus UDP, and the three-way handshake
What are the differences between TCP and UDP? What is the three-way handshake, and why does it take three messages?
```hint Name the mechanism
For each guarantee TCP gives, name the header field or mechanism that provides it; then ask what an application gains by doing without it.
```
```hint Count what must be confirmed
Each side picks a starting sequence number that the other must acknowledge before data can be tracked reliably. Count the messages needed for both numbers to be confirmed.
```
#### What This Part Should Cover
- Connection-oriented reliable byte stream versus connectionless datagrams, and the mechanism behind each TCP guarantee
- Typical uses of each protocol, with reasons
- The SYN, SYN-ACK, ACK exchange with sequence numbers and connection states
- Why two messages are not enough, and what else the handshake negotiates
### Part 2 — Sockets and TCP, and how the kernel finds the right socket
What is the relationship between a socket and TCP? When a TCP packet arrives, which fields does the kernel use to match it to the right connected socket?
```hint Same port, many sockets
A web server has one listening socket on port 443 and thousands of connected sockets that all share local port 443. Ask what still differs between those connections.
```
#### What This Part Should Cover
- The socket as the operating system's endpoint interface, and TCP as one protocol behind it
- The listening socket versus the connected sockets returned by accept
- The lookup key for established connections, the fallback to listening sockets, and how UDP differs
### What a Strong Answer Covers
- Correct mechanics for reliability, ordering, flow control and congestion control
- An accurate handshake with sequence and acknowledgment numbers, and the reason for the third step
- A clear distinction between the API abstraction and the protocol
- The exact lookup key and the two-stage lookup, illustrated with an example
- Awareness of related failure modes such as SYN floods or a full accept queue
### Follow-up Questions
- Why might a busy server accumulate many connections in TIME_WAIT, and which side of a connection ends up there?
- How can several processes listen on the same port with SO_REUSEPORT, and how does the kernel choose among them?
- What happens when a SYN arrives while the server's accept queue is full?
- Why does QUIC run over UDP rather than being a new transport protocol?
Overview: A networking interview question on how TCP differs from UDP, how the three-way handshake works and why it takes three messages, and how sockets relate to TCP. It also tests which packet fields the kernel uses to deliver an incoming TCP segment to the correct connected socket.
Read the full Canonical Software Engineer interview experience this question came from