Module 2 · Beginner · How data travels: network models

The TCP/IP model and how it maps to OSI

The TCP/IP model explained: its four layers and their protocols, how it maps to the 7-layer OSI model, a message through the stack, and Python sockets.

By theprogrammers team · · 9 min read

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 layerJobMain protocolsAddresses used
4. ApplicationWhat the data means to the programHTTP, HTTPS (TLS), DNS, SSH, SMTP, IMAP, FTP, DHCPDomain names, URLs
3. TransportDeliver data between programs on two hostsTCP, UDP, QUICPort numbers
2. InternetGet packets from host to host across networksIPv4, IPv6, ICMPIP addresses
1. Link (network access)Move frames over one physical linkEthernet, Wi-Fi, ARP, PPPMAC 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.

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:

TCPUDP
ConnectionSet up first (a handshake)None
DeliveryReliable, in order, lost data resentBest effort, may be lost or out of order
SpeedMore overheadLighter and faster
Used byWeb pages, APIs, email, SSH, file transfersDNS 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 layerTCP/IP layer
7 ApplicationApplication
6 PresentationApplication
5 SessionApplication
4 TransportTransport
3 NetworkInternet
2 Data linkLink
1 PhysicalLink

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.

OSITCP/IP
Layers74 (or 5)
CreatedDesigned as a standard first (1984), protocols afterwardsProtocols first (1970s), model described from them
Used on the internetNo, only as a reference modelYes
Best forTeaching and troubleshooting vocabularyDescribing how real networks work

A message through the stack

You pay ₹250 at a tea stall with a UPI app. Simplified:

  1. 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.
  2. Transport: TCP (or QUIC) connects to port 443 on that server, splits the request into segments and numbers them.
  3. Internet: each segment gets an IP header with your phone's IP address and the server's IP address.
  4. Link: each packet is put in a frame for the current link: 5G radio frames from your phone to the tower.
  5. 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.
  6. At the server, the layers are unwrapped in reverse: link, internet, transport (segments put back in order), application (request decrypted and processed).
  7. 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 nameLayerMeans
AF_INET / AF_INET6InternetUse IPv4 / IPv6 addresses
SOCK_STREAMTransportA reliable byte stream: TCP
SOCK_DGRAMTransportSeparate datagrams: UDP
The bytes you send()ApplicationWhatever 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

  1. 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).

  1. Which OSI layers does the TCP/IP application layer cover?
Show the answer

OSI layers 5, 6 and 7: session, presentation and application.

  1. 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.

  1. 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.

  1. 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_INET6 choose IPv4 or IPv6 and SOCK_STREAM/SOCK_DGRAM choose 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

  1. Follow the headers at every layer in packets, frames and encapsulation.
  2. Revise the seven layers in the OSI model, layer by layer.
  3. See the transport layer in action in what is a computer network?