Chapter 2 Application Layer
Chapter 2 Application Layer
Application Layer
Computer Networking: A Top-Down
Approach
6th edition
Jim Kurose, Keith Ross
Addison-Wesley
01/22/2024 1
Outline
2.1 Principles of network applications
2.2 Web and HTTP
2.3 FTP
2.4 Electronic Mail (SMTP, POP3, IMAP)
2.5 DNS
2.6 P2P Applications
2.7 Socket programming with UDP and
TCP
01/22/2024 2
Chapter 2: Application Layer
• learn about
Our goals: protocols by
• conceptual, implementation examining popular
aspects of network application application-level
protocols protocols
– HTTP
• transport-layer service models
– FTP
• client-server paradigm – SMTP / POP3 /
• peer-to-peer paradigm IMAP
– DNS
• creating network
applications
– socket API
01/22/2024 3
Some network apps
• e-mail • voice over IP
• web • real-time video conferencing
• instant messaging • cloud computing
• remote login • Social networking
• P2P file sharing • streaming stored video
• multi-user network games (YouTube)
01/22/2024 4
Creating a network app
application
systems
• communicate over network
e.g., web server software
communicates with browser
software
no need to write software
for network-core devices application
applications
network
data link
physical
• applications on end
systems allows for
rapid app development,
propagation
01/22/2024 5
Application architectures
• client-server
• peer-to-peer (P2P)
• hybrid of client-server and P2P
01/22/2024 6
Client-server architecture
server:
• always-on host
• permanent IP address
• data centers for
scaling
clients:
• communicate with
client/server
server
• may be intermittently
connected
• may have dynamic IP
addresses
• do not communicate
01/22/2024
directly with each 7
P2P architecture
• no always-on server
• arbitrary end systems
directly communicate
• peers are intermittently
connected and change IP
addresses
highly scalable but difficult
to manage
01/22/2024 8
Hybrid of client-server and P2P
Skype
• voice-over-IP P2P application
• centralized server: finding address of remote party
• client-client connection: direct (not through server)
Instant messaging
• chatting between two users is P2P
• centralized service: client presence detection/location
• user registers its IP address with central server when it comes online
• user contacts central server to find IP addresses of buddies
01/22/2024 9
Processes communicating
• client process: process
process: program running within a that initiates
host. communication
• within same host, two processes • server process: process
communicate using inter- that waits to be contacted
process communication (defined
by OS). • aside: applications with
P2P architectures have
• processes in different hosts client processes & server
communicate by exchanging processes
messages
01/22/2024 10
Sockets
• process sends/receives messages to/from its socket
• socket analogous to door
• sending process shoves message out door
• sending process relies on transport infrastructure on other side
of door which brings message to socket at receiving process
application application
socket controlled by
process process app developer
transport transport
network network controlled
link
by OS
link Internet
physical physical
01/22/2024 11
Addressing processes • example port
• to receive numbers:
messages, – HTTP server: 80
process must – mail server: 25
have identifier • to send HTTP
• host device has message to
unique 32-bit IP tdtu.edu.vn web
address server:
• identifier – IP address:
128.119.245.12
includes both IP – port number: 80
address and port • A list of well-known
numbers port number can be
01/22/2024 associated with found at 12
App-layer protocol defines
• types of messages open protocols:
exchanged, • defined in RFCs
– e.g., request, • allows for
response interoperability
• message syntax: • e.g., HTTP, SMTP
– what fields in proprietary
messages & how protocols:
fields are • e.g., Skype
described
• message semantics
– meaning of
information in
fields
01/22/2024 13
•
What transport service does
an app need?
data integrity throughput
some apps (e.g.,
• some apps (e.g.,
file transfer, web multimedia)
transactions) require minimum
require 100% amount of
reliable data throughput to be
“effective”
transfer other apps
other apps (e.g.,
•timing (“elastic apps”)
• audio) can (e.g.,
some apps tolerate make use of
some loss
Internet whatever
telephony, security
throughput they
encryption, data
interactive get
games) require integrity, …
low delay to be
01/22/2024
“effective” 14
Transport service requirements
of common apps
01/22/2024 15
Internet transport protocols services
TCP service: UDP service:
• reliable transport
between sending and • unreliable data
receiving process transfer between
• flow control: sending and
sender won’t receiving process
overwhelm receiver
• congestion control: • does not provide:
throttle sender reliability, flow
when network control,
overloaded
• does not provide: congestion
timing, minimum control, timing,
throughput throughput
guarantee, security
guarantee,
• connection-
01/22/2024 oriented: setup security, or 16
required between
Internet apps: application,
transport protocols
application underlying
application layer protocol transport protocol
01/22/2024 17
Securing TCP
TCP & UDP
• no encryption SSL is at app layer
• cleartext passwds sent • Apps use SSL
into socket traverse libraries, which
Internet in cleartext “talk” to TCP
SSL SSL socket API
cleartext passwds
• provides encrypted TCP
sent into socket
connection
traverse Internet
• data integrity encrypted
• end-point authentication See Chapter 7
01/22/2024 18
Outline
2.1 Principles of network applications
2.2 Web and HTTP
2.3 FTP
2.4 Electronic Mail (SMTP, POP3, IMAP)
2.5 DNS
2.6 P2P Applications
2.7 Socket programming with UDP and
TCP
01/22/2024 19
Web and HTTP
First, a review…
web page consists of objects
object can be HTML file, JPEG image, Java applet,
audio file,…
web page consists of base HTML-file which
includes several referenced objects
each object is addressable by a URL
example URL:
www.someschool.edu/someDept/pic.gif
01/22/2024 21
HTTP overview (continued)
01/22/2024 22
HTTP connections
01/22/2024 23
Nonpersistent HTTP
suppose user enters URL: (contains text,
references to 10
www.someSchool.edu/someDepartment/home.index
jpeg images)
1a. HTTP client
initiates TCP 1b. HTTP server at host
connection to HTTP www.someSchool.edu
server (process) at waiting for TCP
www.someSchool.edu on connection at port 80.
port 80 “accepts” connection,
2. HTTP client sends HTTP notifying client
request message
(containing URL) into 3. HTTP server receives
TCP connection socket. request message, forms
Message indicates that response message
client wants object containing requested
someDepartment/home.ind object, and sends
ex message into its socket
time
01/22/2024 24
Nonpersistent HTTP (cont.)
4. HTTP server closes TCP
connection.
5. HTTP client receives
response message
containing html file,
displays html. Parsing
html file, finds 10
time
6. referenced jpeg
Steps 1-5 repeated for
objects
each of 10 jpeg objects
01/22/2024 25
Non-Persistent HTTP: Response time
definition of RTT: time for a small
packet to travel from client to
server and back. initiate TCP
response time: connection
one RTT to initiate TCP RTT
connection request
file
one RTT for HTTP request and time to
RTT
first few bytes of HTTP transmit
file
response to return file
file transmission time received
Non-persistent HTTP
time time
response time
total = 2RTT+transmit time
01/22/2024 26
Persistent HTTP
non-persistent HTTP issues: persistent HTTP
requires 2 RTTs per object server leaves connection
OS overhead for each TCP open after sending
connection response
browsers often open parallel subsequent HTTP messages
TCP connections to fetch between same
referenced objects client/server sent over
open connection
client sends requests as
soon as it encounters a
referenced object
as little as one RTT for all
the referenced objects
01/22/2024 27
HTTP request message
two types of HTTP messages: request, response
HTTP request message:
ASCII (human-readable format)
carriage return character
line-feed character
request line
(GET, POST, GET /index.html HTTP/1.1\r\n
HEAD commands) Host: www-net.cs.umass.edu\r\
n User-Agent:
Firefox/3.6.10\r\n
header Accept: text/
lines html,application/xhtml+xml\r\n
Accept-Language: en-us,en;q=0.5\r\
carriage return, n Accept-Encoding: gzip,deflate\
r\n
line feed at start Accept-Charset: ISO-8859-1,utf-8;q=0.7\r\
of line indicates n Keep-Alive: 115\r\n
end of header lines Connection: keep-alive\r\n
01/22/2024 \r\n 28
HTTP request message:
general format
~
~ entity body ~
~ body
01/22/2024 29
Uploading form input
POST method:
web page often
includes form input
input is uploaded to
server in entity body
URL method:
uses GET method
input is uploaded in
URL field of request
line: www.somesite.com/animalsearch?monkeys&banana
01/22/2024 30
Method types
HTTP/1.0 HTTP/1.1
GET GET, POST, HEAD
POST PUT
HEAD uploads file in entity
asks server to leave body to path specified
in URL field
requested object out of
response DELETE
deletes file specified in
the URL field
01/22/2024 31
HTTP response message
status line
(protocol
status code HTTP/1.1 200 OK\r\n
status phrase) Date: Sun, 26 Sep 201020:09:20 GMT\r\
n Server: Apache/2.0.52
(CentOS)\r\n
Last-Modified: Tue, 30 Oct 2007
header 17:00:02 GMT\r\n
ETag: "17dc6-a5c-bf716880"\r\n
lines
Accept-Ranges: bytes\r\n
Content-Length:2652\r\n
Keep-Alive: timeout=10,
max=100\r\n
Connection: Keep-Alive\r\n
Content-Type: text/html; charset=ISO-
8859- 1\r\n
data, e.g., \r\n
requested data data data data data ...
HTML file
01/22/2024 32
HTTP response status codes
status code appears in 1st line in server->client
response message.
some sample codes:
200 OK
request succeeded, requested object later in this
msg
301 Moved Permanently
requested object moved, new location specified later in this
msg (Location:)
400 Bad Request
request msg not understood by server
404 Not Found
requested document not found on this server
505 HTTP Version Not Supported
01/22/2024 33
Trying out HTTP (client side) for
yourself
ebay 8734
usual http request msg Amazon server
cookie file creates ID
usual http response
1678 for user create backend
ebay 8734
set-cookie: 1678 entry database
amazon 1678
usual http request msg
cookie: 1678 cookie- access
specific
usual http response msg action
cache TT P req
po ns
e
H es
– object in HT TP
r
cache: cache
returns object client origin
server
– else cache
01/22/2024
requests object 38
from origin
More about Web caching
01/22/2024 40
Web Caching Example (1)
Assumptions: origin
• Avg object size: 1 Mbits servers
• Avg request rate from browsers to origin servers: 15 Public
request/sec
Internet
• Avg data rate to browsers: 1.50 Mbps
• RTT from institutional router to any origin server: 2
sec
• Access link rate: 15 Mbps = 100 Mbps
15 Mbps 100 Mbps
Consequences: access link
- (15 req/sec).(1 Mbits/req)/(100 Mbps) = 0.15
Institutional
- (15 req/sec).(1 Mbits/req)/(100 Mbps) = 0.15
network
• Total delay = Internet delay + access delay + LAN 100Mbps LAN
delay
= 2 sec + microseconds+ microseconds
01/22/2024 45
FTP: the file transfer
protocol
file transfer
FTP FTP FTP
user client server
interface
user
at host remote file
local file system
system
01/22/2024 49
Electronic mail
outgoing
Three major components: user message queue
• user agents agent
user mailbox
• mail servers mail user
server
• simple mail transfer agent
protocol: SMTP SMTP mail user
User Agent server agent
• “server”: user
agent
receiving mail
server
01/22/2024 51
Electronic Mail: SMTP [RFC
2821]
• uses TCP to reliably transfer email message
from client to server, port 25
• direct transfer: sending server to
receiving server
• three phases of transfer
• handshaking (greeting)
• transfer of messages
• closure
• command/response interaction (like HTTP,
FTP)
• commands: ASCII text
• response: status code and phrase
• messages must be in 7-bit ASCI
01/22/2024 52
Scenario: Alice sends
message to Bob
1) Alice uses UA to 4) SMTP client sends
compose message “to” Alice’s message over
[email protected] the TCP connection
2) Alice’s UA sends 5) Bob’s mail server
message to her mail places the message in
server; message placed Bob’s mailbox
in message queue 6) Bob invokes his user
3) client side of SMTP agent to read message
opens TCP connection
with Bob’s mail server
01/22/2024 2-54
Try SMTP interaction for
yourself:
• telnet servername 25
• see 220 reply from server
• enter HELO, MAIL FROM, RCPT TO, DATA,
QUIT commands
01/22/2024 2-55
Mail access protocols
user
mail user
SMTP SMTP access
agent agent
protocol
(e.g., POP,
IMAP)
01/22/2024 56
POP3 protocol
S: +OK POP3 server ready
authorization phase C: user bob
S: +OK
• client commands: C: pass hungry
• user: declare username S: +OK user successfully logged on
• pass: password
C: list
• server responses S: 1 498
• +OK S: 2 912
• -ERR S: .
transaction phase,
C: retr 1
S: <message 1 contents>
client: S: .
• list: list message numbers C: dele 1
• retr: retrieve message by C: retr 2
number S: <message 1 contents>
• dele: delete S: .
C: dele 2
• quit
C: quit
S: +OK POP3 server signing off
01/22/2024 57
POP3 (more) and
IMAP
more about POP3 IMAP
• previous example • keeps all messages
uses POP3 in one place: at
“download and server
delete” mode
• allows user to
• Bob cannot re- organize messages
read e-mail if in folders
he changes
client • keeps user state
across sessions:
• POP3 “download-
• names of folders
and-keep”: copies
and mappings
of messages on
between message
different clients
IDs and folder
• POP3 is stateless name
across sessions
01/22/2024 58
Break Time
01/22/2024 59
Outline
2.1 Principles of network applications
2.2 Web and HTTP
2.3 FTP
2.4 Electronic Mail (SMTP, POP3, IMAP)
2.5 DNS
2.6 P2P Applications
2.7 Socket programming with TCP
2.8 Socket programming with UDP
01/22/2024 60
DNS: Domain Name System
people: many Domain Name System:
identifiers: • distributed database
– SSN, name, implemented in
passport # hierarchy of many
Internet hosts, name servers
routers: • application-layer
– IP address (32 protocol: hosts,
bit) - used for name servers
addressing communicate to
datagrams resolve names
– “name”, e.g., (address/name
www.yahoo.com - translation)
used by humans – note: core Internet
01/22/2024
function, 61
DNS: Services, structure
DNS services Why not centralize DNS?
hostname to IP single point of failure
address translation traffic volume
host aliasing distant centralized
Canonical, alias names database
mail server aliasing maintenance
load distribution
replicated Web
doesn’t scale!
servers: set of IP
addresses for one
canonical name
01/22/2024 62
DNS: Distributed, Hierarchical
Database
Root DNS Servers
… …
01/22/2024 63
DNS: Root name servers
• contacted by local name server that can not
resolve name
• root name server:
– contacts authoritative name server if name
mapping not known
– gets mapping
– returns mapping to local
k. RIPE Londonname server
c. Cogent, Herndon, VA (5 other sites)
d. U Maryland College Park, MD (17 other sites)
h. ARL Aberdeen, MD
j. Verisign, Dulles VA (69 other sites ) i. Netnod, Stockholm (37 other sites)
01/22/2024 64
TLD and Authoritative Servers
Top-level domain (TLD) servers:
responsible for com, org, net, edu, aero, jobs,
museums, and all top-level country domains, e.g.:
uk, fr, ca, jp
Network Solutions maintains servers for com
TLD
Educause for edu TLD
Authoritative DNS servers:
organization’s DNS servers, providing
authoritative hostname to IP mappings for
organization’s servers (e.g., Web, mail).
can be maintained by organization or service
01/22/2024
provider 65
Local DNS name server
01/22/2024 2-66
DNS name resolution
example
root DNS server
• host at 2
3
cis.poly.edu TLD DNS server
wants IP address 4
for 5
iterated query:
gaia.cs.umass.ed local DNS server
contacted server
u dns.poly.edu
replies with 7 6
name of server 1 8
to contact
“I don’t know authoritative DNS server
this name, but requesting host
dns.cs.umass.edu
gaia.cs.umass.edu
01/22/2024 67
DNS name resolution example
root DNS server
recursive query:
puts burden of name 2 3
resolution on
contacted name 7 6
server TLD DNS server
heavy load?
local DNS server
dns.poly.edu 5 4
1 8
01/22/2024 g 68
DNS records
type=A type=CNAME
name is hostname name is alias name for some
value is IP address “canonical” (the real)
name
type=NS www.ibm.com is really
• name is domain servereast.backup2.ibm.com
(e.g., foo.com) value is canonical name
• value is hostname of
authoritative name
server for this type=MX
domain value is name of
mailserver associated
with name
01/22/2024 2-69
DNS protocol,
messages
• query and reply messages, both with same message format
2 bytes 2 bytes
01/22/2024 2-70
DNS protocol,
messages
2 bytes 2 bytes
identification flags
01/22/2024 2-72
Attacking DNS
DDoS attacks Redirect attacks
• Bombard root • Man-in-middle
servers with • Intercept queries
traffic • DNS poisoning
• Not successful to • Send bogus relies to DNS
date server, which caches
• Traffic Filtering Exploit DNS for
• Local DNS servers DDoS
cache IPs of TLD
servers, allowing • Send queries with
root server spoofed source address:
bypass target IP
• Bombard TLD • Requires amplification
01/22/2024
servers 2-73
Outline
2.1 Principles of network applications
2.2 Web and HTTP
2.3 FTP
2.4 Electronic Mail (SMTP, POP3, IMAP)
2.5 DNS
2.6 P2P Applications
2.7 Socket programming with TCP
2.8 Socket programming with UDP
01/22/2024 74
Pure P2P
architecture
• no always-on server
• arbitrary end
systems directly
communicate
• peers are
intermittently
connected and
change IP addresses
examples:
• file distribution
(BitTorrent)
• Streaming
(KanKan)
01/22/2024 • VoIP (Skype) 75
File distribution: client-
server vs P2P
Question: how much time to distribute file (size F) from
one server to N peers?
• peer upload/download capacity is limited resource
01/22/2024 76
File distribution time:
client-server
• server transmission:
must sequentially F
us
send (upload) N file di
copies: network
• time to send one copy: ui
F/us
time to
• client: send
eachN client
copies: must download file copy
NF/u
dmins = min client download rate
min client download time: F/dmin
time to distribute F
to N clients using
Dc-s
client-server approach > max{NF/us,,F/dmin}
increases linearly in N
01/22/2024 77
File distribution time:
P2P
• server transmission:
must upload at least
F
us
one copy di
• time to send one network
client: each client
copy: F/u u i
copy
min client download
clients:
time: F/das aggregate must download NF bits
max upload min
rate (limting max download rate) is us +
Sui
time to distribute F
D > max{F/us,,F/dmin,,NF/(us + Sui)}
to N clients using
P2P approachP2P
increases linearly in N …
… but so does this, as each peer brings service capacity
01/22/2024
78
Client-server vs. P2P:
example
client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us
3.5
P2P
1.5
0.5
0
0 5 10 15 20 25 30 35
N
01/22/2024 79
P2P file distribution:
BitTorrent
file divided into 256Kb chunks
peers in torrent send/receive file chunks
tracker: tracks peers torrent: group of
participating in torrent peers exchanging
chunks of a file
Alice arrives …
… obtains list
of peers from tracker
… and begins exchanging
file chunks with peers in torrent
01/22/2024 80
P2P file distribution:
BitTorrent
peer joining torrent:
has no chunks, but
will accumulate
them over time from
other peers
registers with
tracker to get list
of peers, connects
to subset of peers
(“neighbors”)
while downloading, peer uploads chunks to
other peers
peer may change peers with whom it
exchanges chunks
churn: peers may come and go
once peer has entire file, it may
01/22/2024 81
(selfishly) leave or (altruistically)
BitTorrent: requesting, sending
file chunks
01/22/2024 84
2.7 Socket programming
- goal: learn how to build client/server
applications that communicate using sockets
- socket: door between application process and
end-end-transport protocol
application application
socket controlled by
process process app developer
transport transport
network network controlled
link
by OS
link Internet
physical physical
01/22/2024 85
2.7 Socket programming
Two socket types for two transport
services:
– UDP: unreliable datagram
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
01/22/2024 88
Example app: UDP client
Python UDPClient.py
include Python’s socket
library from socket import *
serverName = ‘hostname’
serverPort = 12000
create UDP socket for clientSocket = socket(socket.AF_INET,
server
01/22/2024 89
Example app: UDP server
Python UDPServer
from socket import *
serverPort = 12000
create UDP socket serverSocket = socket(AF_INET, SOCK_DGRAM)
bind socket to local port
number 12000
serverSocket.bind(('', serverPort))
print “The server is ready to receive”
loop forever while 1:
Read from UDP socket into message, clientAddress = serverSocket.recvfrom(2048)
message, getting client’s
address (client IP and port) modifiedMessage = message.upper()
send upper case string serverSocket.sendto(modifiedMessage, clientAddress)
back to this client
01/22/2024 90
Socket-programming using TCP
Socket: a door between application process and end-
end-transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
controlled by process application
application process
developer
developer socket socket
TCP with TCP with controlled by
controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
01/22/2024 91
Socket programming
client must contact
with TCP
• when contacted by
server client, server TCP
creates new socket for
• server process must
server process to
first be running
communicate with that
• server must have
particular client
created socket (door) – allows server to
that welcomes
talk with multiple
client’s contact
clients
client contacts – source port numbers
server by: used to distinguish
application viewpoint:
clients (more in
• Creating TCP socket,
specifying IP TCP provides reliable, in-order
Chap 3)
address, port numberbyte-stream transfer (“pipe”)
of server process between client and server
01/22/2024 • when client creates 92
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
01/22/2024 93
Client/server socket interaction: TCP
01/22/2024 94
Example app: TCP client
Python TCPClient
from socket import *
serverName = ’servername’
create TCP socket for serverPort = 12000
server, remote port 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
No need to attach server clientSocket.send(sentence)
name, port
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
01/22/2024 95
Example app: TCP
Python server
TCPServer
from socket import *
create TCP welcoming
serverPort = 12000
socket serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
server begins listening for
incoming TCP requests
serverSocket.listen(1)
print ‘The server is ready to receive’
loop forever while 1:
server waits on accept() connectionSocket, addr = serverSocket.accept()
for incoming requests, new
socket created on return
sentence = connectionSocket.recv(1024)
read bytes from socket (but
not address as in UDP) capitalizedSentence = sentence.upper()
close connection to this
connectionSocket.send(capitalizedSentence)
client (but not welcoming connectionSocket.close()
socket)
01/22/2024 96
Chapter 2: of
our study Summary
network apps now
• complete!
application architectures specific
– client-server protocols:
– P2P HTTP
FTP
SMTP, POP, IMAP
• application service
requirements: DNS
– reliability, bandwidth, P2P: BitTorrent,
DHT
delay
OS
packet Transport (TCP/UDP)
01/22/2024 99
Exercises
Ex1. You use Wireshark to investigate and
explore several aspects of the HTTP
protocol:
the basic GET/reply interaction,
HTTP message formats,
retrieving large HTML files,
retrieving HTML files with embedded URLs,
persistent and non-persistent connections,
and HTTP authentication and security
01/22/2024 100