The OSI model has seven neat layers, but the internet you use every day doesn't run on OSI. It runs on TCP/IP: the family of protocols, built in the 1970s for the US research network ARPANET, that every phone, laptop and server speaks today. Its model has only four layers, and when you open a socket in Python, configure a cloud firewall or read a packet in Wireshark, it's TCP/IP's layers you're working with.
This lesson explains the TCP/IP model: its four layers and the protocols in each, how it maps to OSI, what really happens to a UPI payment or a WhatsApp message as it passes through the stack, and how the layers show up in Python's socket module.
What you need first: the OSI model, layer by layer.
Key terms
- TCP/IP: the protocol suite the internet uses, named after its two most important protocols, TCP and IP.
- Protocol suite (stack): a family of protocols designed to work together in layers.
- Network stack: the part of the operating system that implements TCP/IP; apps hand it data and it does the rest.
- RFC (request for comments): the documents that define internet protocols, published by the IETF.
- End-to-end principle: keep the network simple (just move packets) and put intelligence, like reliability, in the end devices.
The four layers
| TCP/IP layer | Job | Main protocols | Addresses used |
|---|---|---|---|
| 4. Application | What the data means to the program | HTTP, HTTPS (TLS), DNS, SSH, SMTP, IMAP, FTP, DHCP | Domain names, URLs |
| 3. Transport | Deliver data between programs on two hosts | TCP, UDP, QUIC | Port numbers |
| 2. Internet | Get packets from host to host across networks | IPv4, IPv6, ICMP | IP addresses |
| 1. Link (network access) | Move frames over one physical link | Ethernet, Wi-Fi, ARP, PPP | MAC addresses |
Some books number them in the other direction, or split the link layer into "data link" and "physical" to make a five-layer model. The ideas are the same.
Link layer
Moves data across one link: from your laptop to the Wi-Fi router, or from one router to the next. It covers both OSI layers 1 and 2: the cable or radio, and the frames and MAC addresses on it. TCP/IP deliberately says little about this layer, so IP can run over anything: Ethernet, Wi-Fi, 4G, 5G, fibre, even satellite links.
Internet layer
The heart of the internet: IP gives every host an address and every packet a source and destination, and routers forward packets hop by hop towards the destination. IP is best effort: it tries to deliver each packet but promises nothing. Packets can be lost, duplicated or arrive out of order. ICMP carries error reports and the messages ping and traceroute use.
Transport layer
Gets data from a program on one host to a program on another, using ports:
| TCP | UDP | |
|---|---|---|
| Connection | Set up first (a handshake) | None |
| Delivery | Reliable, in order, lost data resent | Best effort, may be lost or out of order |
| Speed | More overhead | Lighter and faster |
| Used by | Web pages, APIs, email, SSH, file transfers | DNS lookups, video calls, live streams, online games |
Because IP is unreliable, TCP adds reliability in the end hosts. Routers in the middle stay simple. That's the end-to-end principle, and it's a big reason the internet could grow so large.
QUIC, used by HTTP/3, is a newer transport protocol built on top of UDP that gives TCP-like reliability with faster connection set-up.
Application layer
Everything the program cares about: requesting a page (HTTP), looking up a name (DNS), logging in to a server (SSH), sending email (SMTP). TLS encryption also lives here in TCP/IP terms, between the application's protocol and TCP.
How TCP/IP maps to OSI
| OSI layer | TCP/IP layer |
|---|---|
| 7 Application | Application |
| 6 Presentation | Application |
| 5 Session | Application |
| 4 Transport | Transport |
| 3 Network | Internet |
| 2 Data link | Link |
| 1 Physical | Link |
OSI's top three layers become TCP/IP's one application layer, and OSI's bottom two become the link layer. The middle (transport and network/internet) matches exactly, which is why "layer 3" and "layer 4" mean the same thing in both.
| OSI | TCP/IP | |
|---|---|---|
| Layers | 7 | 4 (or 5) |
| Created | Designed as a standard first (1984), protocols afterwards | Protocols first (1970s), model described from them |
| Used on the internet | No, only as a reference model | Yes |
| Best for | Teaching and troubleshooting vocabulary | Describing how real networks work |
A message through the stack
You pay ₹250 at a tea stall with a UPI app. Simplified:
- Application: the app builds an HTTPS request with the payment details, encrypted with TLS, to the payment provider's server name. DNS first turns that name into an IP address.
- Transport: TCP (or QUIC) connects to port 443 on that server, splits the request into segments and numbers them.
- Internet: each segment gets an IP header with your phone's IP address and the server's IP address.
- Link: each packet is put in a frame for the current link: 5G radio frames from your phone to the tower.
- The packets cross the mobile network, the ISP and the internet. At each router, the link-layer frame is removed and replaced with a new one for the next link, but the IP packet inside stays the same.
- At the server, the layers are unwrapped in reverse: link, internet, transport (segments put back in order), application (request decrypted and processed).
- The confirmation comes back the same way, and your app shows "Payment successful".
The routers in step 5 only ever look at the link and internet layers. They never see the payment details, both because they don't need to and because TLS has encrypted them.
TCP/IP in Python's socket API
When you create a socket in Python, you choose the internet layer protocol and the transport protocol; the OS network stack handles everything below:
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
udp_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
print(tcp_socket.family.name, tcp_socket.type.name)
print(udp_socket.family.name, udp_socket.type.name)
tcp_socket.close()
udp_socket.close()
AF_INET SOCK_STREAM
AF_INET6 SOCK_DGRAM
| Python name | Layer | Means |
|---|---|---|
AF_INET / AF_INET6 | Internet | Use IPv4 / IPv6 addresses |
SOCK_STREAM | Transport | A reliable byte stream: TCP |
SOCK_DGRAM | Transport | Separate datagrams: UDP |
The bytes you send() | Application | Whatever protocol your program speaks, such as HTTP |
You never write the link layer yourself; the OS and network card do it. Well-known application protocols also have standard port numbers, which Python can look up:
import socket
for service, transport in [("http", "tcp"), ("https", "tcp"), ("ssh", "tcp"), ("domain", "udp")]:
print(service, transport, socket.getservbyname(service, transport))
http tcp 80
https tcp 443
ssh tcp 22
domain udp 53
domain is the official service name for DNS. The ports module of this course explains port numbers fully.
Practice
- Name the four TCP/IP layers from top to bottom, with one protocol at each.
Show the answer
Application (HTTP), transport (TCP), internet (IP), link (Ethernet or Wi-Fi).
- Which OSI layers does the TCP/IP application layer cover?
Show the answer
OSI layers 5, 6 and 7: session, presentation and application.
- A router forwards your packet. Which TCP/IP layers does it process, and what does it change?
Show the answer
The link layer and the internet layer. It removes the incoming frame, reads the IP destination, chooses the next hop, and wraps the packet in a new frame for the outgoing link. (It also lowers the IP header's time-to-live by one.) It doesn't read the transport or application data.
- Why does DNS usually use UDP, while loading a web page uses TCP?
Show the answer
A DNS lookup is one small question and one small answer; UDP is faster, and if the reply is lost the client just asks again. A web page is larger and must arrive complete and in order, which TCP guarantees.
- In Python, which socket would you create for a UDP program over IPv4?
Show the answer
socket.socket(socket.AF_INET, socket.SOCK_DGRAM): AF_INET for IPv4 at the internet layer, SOCK_DGRAM for UDP at the transport layer.
Interview questions
What is the difference between the OSI and TCP/IP models?
OSI is a seven-layer reference model designed before its protocols; TCP/IP is a four-layer model derived from the protocols the internet actually uses. TCP/IP merges OSI's application, presentation and session layers into one application layer, and OSI's data link and physical layers into one link layer. Transport and network/internet layers correspond directly.
Why is IP called a best-effort protocol?
IP tries to deliver each packet but guarantees nothing: packets may be lost, duplicated, corrupted or arrive out of order, and IP doesn't retransmit them. Reliability, where needed, is added by transport protocols such as TCP in the end hosts.
What happens at each TCP/IP layer when you send an HTTP request?
The application layer forms the HTTP request (encrypted by TLS for HTTPS). The transport layer (TCP) segments it, adds ports and sequence numbers, and manages reliable delivery. The internet layer adds IP source and destination addresses and routes packets. The link layer frames each packet with MAC addresses for the current hop and sends it as signals.
Key takeaways
- TCP/IP is the protocol suite the internet runs on, with four layers: application, transport, internet and link.
- The internet layer (IP) moves packets between hosts on a best-effort basis; the transport layer (TCP or UDP) delivers to programs using ports.
- TCP/IP's application layer covers OSI layers 5 to 7; its link layer covers OSI layers 1 and 2.
- Routers process only the link and internet layers; the end hosts handle transport and application.
- In Python,
AF_INET/AF_INET6choose IPv4 or IPv6 andSOCK_STREAM/SOCK_DGRAMchoose TCP or UDP; the OS does the rest.
Frequently asked questions
Why is it called TCP/IP?
After its two central protocols: TCP (Transmission Control Protocol) for reliable delivery and IP (Internet Protocol) for addressing and routing. The name covers the whole family, including UDP, DNS, HTTP and the rest.
Is TCP/IP a 4-layer or 5-layer model?
The original description (RFC 1122) has four layers. Many textbooks use five, splitting the link layer into data link and physical, which matches hardware better. Both describe the same protocols.
Which model should I learn?
Both. Use TCP/IP to understand how the internet actually works, and OSI's layer numbers to talk to other engineers and to troubleshoot.
What to learn next
- Follow the headers at every layer in packets, frames and encapsulation.
- Revise the seven layers in the OSI model, layer by layer.
- See the transport layer in action in what is a computer network?