Sub topic 1
Why Networks Are Built in Layers
Prerequisites
This chapter assumes that you know what clients and servers are, have basic internet familiarity, and can run terminal commands.
Why Networks Are Built in Layers
A network has many independent jobs: sending signals, addressing devices, choosing routes, delivering data to programs, and interpreting application messages. Layering divides these jobs into cooperating parts.
Each layer provides services to the layer above it and uses the layer below it. This separation makes protocols easier to design, replace, and troubleshoot. For example, HTTP can work over different link technologies, and Ethernet can carry many internet protocols.
The OSI Reference Model: Seven Layers
The OSI model is a seven-layer reference model. It is mainly a vocabulary for describing network functions:
- Application: Network services used by applications, such as HTTP and DNS.
- Presentation: Data format, encryption, and compression.
- Session: Conversations between applications.
- Transport: End-to-end delivery, reliability, and ports. TCP and UDP operate here.
- Network: Logical addressing and routing. IP operates here.
- Data link: Local delivery using frames and MAC addresses. Ethernet operates here.
- Physical: Electrical, radio, or optical signals that carry bits.
The OSI layers describe responsibilities. Real protocols do not always fit one layer perfectly.
The TCP/IP Model: Four Layers
The TCP/IP model describes the protocol suite used by the internet:
| TCP/IP layer | Main job | Examples |
|---|---|---|
| Application | Application protocols and data formats | HTTP, DNS, TLS |
| Transport | Process-to-process delivery | TCP, UDP |
| Internet | Addressing and routing between networks | IP, ICMP |
| Link | Delivery across one local network | Ethernet, Wi-Fi, ARP |
The TCP/IP application layer combines the OSI application, presentation, and session layers. Its link layer generally combines the OSI data-link and physical layers.
Encapsulation and Decapsulation
When an application sends data, each lower layer adds a header. This process is encapsulation. At the destination, each layer removes and processes its header. This is decapsulation.
flowchart TD
A[Application data] --> B[Transport: segment or datagram]
B --> C[Internet: IP packet]
C --> D[Link: frame]
D --> E[Physical signals]
E --> F[Destination decapsulates upward]For example, a web request becomes an HTTP message inside a TCP segment, inside an IP packet, inside an Ethernet frame. The receiving host reverses those steps.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.sendto(b"hello", ("127.0.0.1", 9999))The program supplies application data, an IP address, and a UDP port. The operating system adds the transport, internet, and link-layer information.
Exercise
Which TCP/IP layer normally handles routing between separate networks?
Show answer
The internet layer, using IP addresses and routing.