Each layer in the protocol stack encapsulates data from the layer above by prepending its own header, creating a nested structure of headers and payload. When data moves down the stack, each layer adds headers; when it moves up, each layer removes its headers. This encapsulation allows protocols to operate independently while maintaining the abstraction boundaries that define the layered architecture.
From the OSI model, you know that network communication is organized into layers, each with a defined responsibility — the application layer deals with user data, the transport layer with reliable delivery, the network layer with routing, and so on. Encapsulation is the mechanism that makes this layered architecture actually work in practice. It is the process by which each layer wraps the data it receives from the layer above with its own header (and sometimes a trailer), treating everything from above as an opaque payload.
Picture a letter being mailed. You write a message (application data) and put it in an envelope with a "To" and "From" address (transport header — port numbers for source and destination processes). That envelope goes into a larger envelope with street addresses (network header — IP addresses for source and destination hosts). That package goes into a mail bag with routing labels for the postal system (data link header — MAC addresses for the next hop). At each stage, the handler at that level only reads *their* envelope — the mail carrier doesn't open your letter, and the postal sorting facility doesn't care what the carrier's route is. This is encapsulation: each layer's information is independent and self-contained.
In concrete terms, when an application sends an HTTP message, the transport layer (TCP) prepends a TCP header containing source and destination port numbers, sequence numbers, and flags, creating a segment. The network layer (IP) then wraps the entire segment — TCP header and all — in an IP header containing source and destination IP addresses, creating a packet. The data link layer wraps the entire packet in a frame header (with MAC addresses) and appends a frame check sequence trailer for error detection, creating a frame. Finally, the physical layer converts the frame into bits on a wire or radio signal. At each step, the lower layer has no knowledge of or dependency on the internal structure of what it carries — to IP, a TCP segment is just bytes; to Ethernet, an IP packet is just bytes.
On the receiving end, the process reverses. The data link layer strips its header and trailer, checks for errors, and passes the payload (the IP packet) up. The network layer strips the IP header, reads the destination, and passes the payload (the TCP segment) up. TCP strips its header, reassembles the data stream, and delivers the application payload. This symmetric wrapping and unwrapping is what allows any application protocol to run over any transport protocol, over any network protocol, over any link technology. You can swap Ethernet for WiFi at layer 2 without touching anything above — because each layer's encapsulation creates a clean boundary. This is the engineering payoff of layered design: independent evolution and interchangeability of protocols at each level.
No topics depend on this one yet.