Computer Communication Networks
Application Layer — Complete Study Notes
CUST Islamabad | Instructor: Dr. Jehan Zeb
1. Principles of Network Applications
Network applications run on end systems (hosts) and communicate over the network. You only write
software for end systems — not for routers or switches in the network core. This separation allows
rapid app development.
Client-Server Paradigm
• Server: always-on host, permanent IP address, often in data centers for scaling
• Clients: contact server, may be intermittently connected, may have dynamic IPs
• Examples: HTTP, IMAP, FTP
Peer-to-Peer (P2P)
• No always-on server — arbitrary end systems communicate directly
• Self-scalable: new peers bring new service capacity as well as new demand
• Peers are intermittently connected with changing IP addresses — complex management
• Example: BitTorrent
Processes and Sockets
Process: A program running on a host. Processes on different hosts communicate via messages.
Socket: The interface between application and transport layers — analogous to a door. Process
pushes/receives messages through it.
Process identifier: IP address + Port number. E.g., HTTP server uses port 80, mail server uses port
25.
Transport Service Requirements
Requirement Description Example Apps
Reliable transfer 100% delivery, no data loss File transfer, web, email
Throughput Minimum guaranteed bandwidth Video streaming, telephony
Timing Low delay required Online games, VoIP
Security Encryption, data integrity Banking, secure web
Loss-tolerant Can accept some data loss Audio/video streaming
TCP vs UDP
Feature TCP UDP
Reliability Reliable, in-order delivery Unreliable
Flow control Yes — prevents overwhelming No
receiver
Congestion control Yes — throttles when network No
congested
Connection setup Yes (3-way handshake) No
Timing/throughput No guarantee No guarantee
Security Via TLS (application layer) None built-in
Use cases HTTP, SMTP, FTP DNS, streaming, gaming
Note: TLS (Transport Layer Security) is implemented at the application layer, on top of TCP, to provide
encrypted connections.
2. Web and HTTP
A web page consists of a base HTML file plus referenced objects (images, scripts, etc.), each stored at
a URL: hostname/pathname. HTTP (HyperText Transfer Protocol) is the web's application-layer
protocol running over TCP (port 80). HTTP is stateless — the server keeps no memory of past
requests.
HTTP Connection Types
Non-Persistent HTTP
One object per TCP connection. Response time per object = 2RTT + file transmission time. For a page
with 10 objects: at minimum 21 RTT worth of overhead plus transmission times.
Non-persistent delay = 2RTT + file_transmission_time (per object)
Persistent HTTP (HTTP/1.1)
• Server leaves connection open after sending response
• Multiple objects sent over a single TCP connection
• With pipelining: client sends all requests without waiting — the entire batch adds ~1 RTT total
overhead
• Without pipelining: still ~1 RTT per object plus data transfer time
HTTP Request Methods
Method Purpose
GET Retrieve a resource; can include data after '?' in URL
POST Send data to server in request body (e.g., form submissions)
HEAD Like GET but returns only headers — used to check existence/metadata without
downloading
PUT Upload/replace a file at the specified URL
HTTP Response Status Codes
Code Meaning
200 OK Request succeeded, object returned
301 Moved Permanently Object at new location (given in Location header)
400 Bad Request Server could not understand request
404 Not Found Requested document not on server
505 HTTP Version Not Server does not support the HTTP version used
Supported
Cookies
Used to maintain state across stateless HTTP. Four components: (1) cookie header in HTTP response,
(2) cookie header in future HTTP requests, (3) cookie file managed by browser on client, (4) backend
database at web server.
• Uses: authorization, shopping carts, recommendations, session state
• Privacy: cookies let sites track behavior; subject to GDPR when they can identify an individual
Web Caching (Proxy Servers)
A cache satisfies client requests without involving the origin server. Acts as both server (to client) and
client (to origin server).
• Reduces response time — cache is physically closer to client
• Reduces traffic on the institution's access link
Caching Formula (Assignment Q2)
Model total average response time as: access delay + Internet delay.
Access delay = Delta / (1 - Delta * beta)
Where: Delta = L/R (time to transmit one object over access link), beta = arrival rate of objects to
access link
With a cache (miss rate m):
Total response time = m * (Internet delay + access delay) + (1-m) *
(small cache delay)
Example: miss rate = 0.4, Internet delay = 3s. Only 40% of requests go to the internet, drastically
reducing access link utilization and average delay.
3. Email: SMTP and IMAP
Three Major Components
• User Agents (UA): mail clients like Outlook, iPhone Mail — compose, send, read
• Mail Servers: hold user mailboxes and outgoing message queues
• SMTP (Simple Mail Transfer Protocol): transfers mail between servers
SMTP (RFC 5321)
• Uses TCP port 25
• Client push model (contrast with HTTP which is client pull)
• Three phases: handshaking (HELO), message transfer (DATA), closure (QUIT)
• Commands and responses are ASCII text
• Requires 7-bit ASCII for message body
• Uses persistent connections; multiple objects in a multipart message
Email Flow: Alice to Bob
1. Alice composes message in her User Agent
2. UA sends message to Alice's mail server via SMTP; placed in message queue
3. Alice's mail server opens TCP connection to Bob's mail server
4. SMTP client sends Alice's message over the TCP connection
5. Bob's mail server places message in Bob's mailbox
6. Bob invokes his UA using IMAP or POP3 to read the message
Mail Access Protocols
• IMAP (RFC 3501): messages stored on server; supports retrieval, deletion, folders
• HTTP: webmail interfaces (Gmail, [Link]) use SMTP to send and IMAP/POP to retrieve
4. DNS — Domain Name System
DNS translates human-readable hostnames (e.g., [Link]) into 32-bit IP addresses. It is a distributed
database implemented as a hierarchy of name servers — also an application-layer protocol.
Why Distributed? (Not Centralized)
• Single point of failure if centralized
• Enormous traffic volume — Akamai alone handles 2.2 trillion DNS queries/day
• Distant centralized database causes high latency
• Maintenance burden across billions of records
DNS Hierarchy
Level Role
Root DNS Servers Top of hierarchy; points to TLD servers
TLD Servers Responsible for .com, .org, .edu, country codes (.pk, .uk, etc.)
Authoritative DNS Servers Organization's own DNS; provides final hostname-to-IP mappings
Local DNS Server Each ISP has one; first stop for client queries; acts as proxy/cache
Query Types
Iterated Query
The local DNS server does all the work — contacts root, TLD, authoritative servers in turn and
assembles the answer for the client.
Recursive Query
Each name server resolves the query on behalf of the previous one, passing the burden up. Puts heavy
load at the top of the hierarchy.
DNS Caching
• Any name server that learns a mapping caches it for TTL (Time to Live) duration
• TLD servers typically cached in local name servers
• Cached entries can go stale if a host changes IP before TTL expires
• Caching improves response time significantly
DNS Resource Records (RR)
Format: (name, value, type, TTL)
Type Name Value
A Hostname (e.g., [Link]) IP address
NS Domain (e.g., [Link]) Hostname of authoritative name server
CNAME Alias name Canonical (real) name
MX Domain name Hostname of mail server for that domain
Full DNS Resolution: [Link]
Step 1: Browser cache → Step 2: OS cache → Step 3: Local DNS resolver cache
Step 4: Root server (points to .com TLD) → Step 5: TLD server (points to authoritative)
Step 6: Authoritative DNS (returns IP) → Step 7: IP cached at all levels for future use
DNS Security
• DDoS attacks: bombard root or TLD servers with traffic — traffic filtering used as defense
• Spoofing/Cache Poisoning: intercept DNS queries and return bogus replies
• DNSSEC (RFC 4033): provides authentication services to combat spoofing
5. Message Segmentation (Assignment Q1)
Without Segmentation
A message of size M bits over N links at rate R takes N x (M/R) seconds end-to-end. Each router uses
store-and-forward — it must receive the entire message before forwarding.
Total delay (no segmentation) = N * (M / R)
With Segmentation — Pipelining Formula
With T packets of L bits each, over N links at rate R, the delay for the n-th packet is:
d(n) = N * (L/R) + (n - 1) * (L/R)
Where: n = 1 to T, L = packet length, R = link rate, N = number of links
Total delay (all T packets delivered) — put n = T:
d(T) = (N + T - 1) * (L/R)
Assignment Q1 Worked Solution
Given: M = 8x10^6 bits, 2 routers (3 links), R = 2 Mbps each
Part a — No Segmentation
Time to reach first router = M/R = 8,000,000 / 2,000,000 = 4 seconds
Total = 3 * (8,000,000 / 2,000,000) = 3 * 4 = 12 seconds
Part b — Segmented (T=800, L=10,000 bits)
First packet to first router = L/R = 10,000 / 2,000,000 = 0.005 seconds
Second packet fully received by second router: d(2) for n=2 considering pipelining — the second packet
arrives at the first router while the first packet is being forwarded.
Part c — Total time with segmentation
d(T) = (3 + 800 - 1) * (10,000 / 2,000,000) = 802 * 0.005 = 4.01
seconds
Segmentation reduces total delay from 12 seconds to 4.01 seconds — nearly 3x faster.
Advantages of Message Segmentation
• Pipelining: links are utilized in parallel, reducing total delay
• Error recovery: only the lost/corrupted packet is retransmitted, not the whole message
• Multiplexing: different users' packets can share links fairly
Disadvantages of Message Segmentation
• Header overhead on every packet reduces effective throughput
• Out-of-order arrival requires reassembly buffers at destination
• Increased processing complexity at every router
6. P2P File Distribution (Assignment Q4)
Client-Server vs P2P
In client-server distribution, the server must upload to every peer — bottleneck grows linearly with N. In
P2P, each peer that downloads a chunk can re-upload it to others.
Distribution Time Formulas
Client-Server: T_cs = max(N*F/Us, F/d_min)
P2P: T_p2p = max(F/Us, F/d_min, N*F / (Us + sum_of_Ui))
Where: F = file size, Us = server upload rate, d_min = minimum peer download rate, Ui = upload rate of
peer i
Assignment Q4 Worked Solution
Given: F = 15 Gbits = 15,000 Mbits, Us = 30 Mbps, D = 2 Mbps, U = 700 Kbps = 0.7 Mbps
N T_cs (seconds) T_p2p (seconds) Winner
10 max(10*15000/30, 15000/2) = max(15000/30, 15000/2, Similar
max(5000, 7500) = 7500 s 10*15000/(30+7)) = max(500, 7500,
4054) = 7500 s
100 max(100*15000/30, 7500) = max(500, 7500, 100*15000/(30+70)) P2P wins
max(50000, 7500) = 50000 s = max(500, 7500, 15000) = 15000 s
As N increases, client-server time grows linearly (server must serve everyone alone). P2P time grows
much more slowly because aggregate upload capacity (Us + N*U) also grows with N. At large N, P2P is
dramatically faster and more scalable.
7. Socket Programming
Overview
Sockets are the API between application and transport layers, controlled by the OS. Two main types:
UDP (SOCK_DGRAM, connectionless) and TCP (SOCK_STREAM, connection-oriented, reliable byte-
stream).
UDP Socket Interaction
• No connection setup — client attaches server name and port to each datagram
• Server binds to a port and reads from the socket; source address retrieved with each datagram
• Simple but unreliable — no guarantee of delivery or order
TCP Socket Interaction
• Server creates a welcoming socket, calls listen(), then accept() — blocks until client connects
• accept() returns a new dedicated connection socket for that specific client
• Client calls connect() — triggers TCP three-way handshake automatically
• TCP provides a reliable, in-order byte-stream pipe between client and server
• Multiple clients handled simultaneously: each gets its own connection socket
Socket Timeouts
Use settimeout(n) to raise a timeout exception if no data arrives within n seconds on any subsequent
socket operation on that specific socket.
• settimeout(None) — removes timeout (blocking mode)
• settimeout(0) — non-blocking mode (different behavior, raises exception immediately if no data)
• Timer starts when recv() is called; resets on each call
Python try-except Pattern for Timeouts
[Link](10) try: data = [Link](1024) except timeout: # handle
timeout — no data in 10 seconds
8. Key Formulas Summary
Formula Expression Used For
Non-persistent HTTP 2RTT + file_tx_time Per-object fetch time
delay
Access link utilization Delta * beta Queuing analysis
Access delay Delta / (1 - Delta*beta) Web cache problems
E2E delay, 1 packet, N N * (L/R) Single packet forwarding
links
E2E delay, n-th packet N*(L/R) + (n-1)*(L/R) Segmented transmission
(pipelined)
Total delay, T packets, N (N + T - 1) * (L/R) Full message delivery
links
Client-server distribution max(N*F/Us, F/d_min) File distribution
P2P distribution max(F/Us, F/d_min, P2P file sharing
N*F/(Us+sum_Ui))
CCN Application Layer Notes | CUST Islamabad | Based on course slides and Assignment #01