Transport Layer – End-to-End Delivery
The transport layer ensures end-to-end delivery of data. When a client sends data to another client or requests data from a server, the data is transmitted in the form of packets, which contain two IP addresses:
- Sender's IP address
- Receiver's IP address
Now, imagine a scenario where a user has multiple tabs open (Google, YouTube, LinkedIn, etc.). When data is received, how does the system know which tab requested it and where to deliver it?
This is determined by the port number, as each tab operates on a unique port. When data is delivered, it comes with a destination port number, ensuring that it reaches the correct application. The port number is 16-bit, meaning there are 2¹⁶ (65,536) possible ports. This is why we say that the transport layer provides port-to-port delivery.
For example, if you are requesting data from YouTube over HTTPS, then the request uses port 443 (which is the default port for HTTPS). When the packet is sent, it includes:
- Sender’s IP address
- Sender’s port number (tab’s port)
- Receiver’s IP address
- Receiver’s port number (443 for HTTPS in this case)
When the data reaches the server, it looks at the port number and routes the request accordingly (e.g., to the HTTPS service). The server then sends the response back with these same four details to ensure correct delivery.
Data Flow Through Layers
- Application Layer: Data is created.
- Transport Layer: Adds port numbers → Now called a segment.
- Network Layer: Adds IP addresses → Now called a packet.
- Data Link Layer: Adds MAC addresses → Now called a frame.
- Physical Layer: Converts frames into bits for transmission.
TCP vs UDP
1. TCP (Transmission Control Protocol) – Reliable Communication
TCP is a connection-oriented protocol. Before sending data, it establishes a connection using a three-way handshake:
- Client → Server: "Are you available?" (SYN)
- Server → Client: "Yes, I am available!" (SYN-ACK)
- Client → Server: "Okay, I am sending data now." (ACK)
Once the connection is established, TCP ensures reliable delivery by keeping track of packets. If a packet is lost, the receiver requests it again.
For example, if 100MB of data is divided into 20 segments, but only 18 arrive, the receiver will request the missing 2 packets before assembling the complete data. This makes TCP reliable for scenarios like emails, file downloads, and web browsing.
2. UDP (User Datagram Protocol) – Faster, but Unreliable
UDP is connectionless, meaning it does not establish a connection before sending data. It simply starts transmitting packets without checking whether they reach the destination.
Since UDP does not retransmit lost packets, it is faster than TCP and is used for real-time applications like:
- Live streaming
- Online gaming
- Video conferencing
In these cases, speed is more important than accuracy. If a few packets are lost during a video call, you might notice a slight glitch, but the conversation continues without delays.
Network Address Translation (NAT) and Private/Public IPs
In a LAN (Local Area Network), each device has a private IP address, but they all share a common public IP address assigned by the router when connecting to the internet.
Why do we need NAT?
When multiple devices in a LAN request data from an external server, the router replaces the private IP address of the sender with the public IP before forwarding the request. However, when the response comes back, the router needs to determine which device in the LAN requested the data.
This is where NAT (Network Address Translation) helps:
- When a request goes out, NAT assigns a unique port number to each request.
- The router stores a table mapping these unique port numbers to the original private IP addresses.
- When the response comes back, NAT checks the port number and forwards the data to the correct private IP.
Example
-
Device A (192.168.1.10) → Requests data from
example.com
- NAT assigns Port 5001
- Sends request:
(Public IP, Port 5001) → example.com
-
Device B (192.168.1.20) → Requests data from
example.com
- NAT assigns Port 5002
- Sends request:
(Public IP, Port 5002) → example.com
When the server responds, the router looks at the port number and forwards the response to the correct device.
Why Not Use MAC Addresses?
MAC addresses change when data moves across different networks. While MAC addresses work within a local network, they cannot be used for routing data over the internet because routers do not forward MAC addresses beyond a local network. That’s why IP addresses are used for identifying devices across different networks.
Session Layer – Authentication & Authorization
The session layer manages user sessions, authentication, and authorization.
- Authentication: Verifies who you are (e.g., logging into Instagram with a username and password).
- Authorization: Determines what you can do (e.g., you can view your bank balance, but you cannot modify it unless you have special permissions).
It also handles session checkpoints, which allow downloads to resume from where they stopped instead of restarting from the beginning. Session hijacking can occur if an attacker steals your session ID (from cookies).
Presentation Layer – Data Formatting & Security
The presentation layer ensures data is formatted correctly for different systems.
- Encryption/Decryption: Secures communication (e.g., SSL/TLS encrypt HTTPS traffic).
- Data Compression: Reduces file sizes for faster transfer.
- Data Translation: Converts formats like ASCII to UTF-8 if two systems use different encoding.
Application Layer – User Interaction
The application layer provides network services directly to users.
Common Protocols
- HTTP (HyperText Transfer Protocol): Transfers web pages.
- HTTPS (Secure HTTP): Encrypted web browsing.
- SMTP (Simple Mail Transfer Protocol): Sends emails.
- FTP (File Transfer Protocol): Transfers files.
Network Transparency
When users browse the internet, they only see the UI (User Interface) of applications. They do not need to understand the underlying network mechanisms handling requests, data transfer, and delivery.