Professional Documents
Culture Documents
Intro CN Ch02 Class
Intro CN Ch02 Class
Intro CN Ch02 Class
Application Layer
2: Application Layer 2
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP Building a Web server
2.5 DNS by Unix socket
programming
2: Application Layer 3
Some network apps
e-mail voice over IP
web real-time video
instant messaging conferencing
remote login grid computing
P2P file sharing …
multi-user network …
games …
streaming stored video
clips
2: Application Layer 4
Creating a network app application
transport
network
data link
2: Application Layer 6
Application architectures
Client-server
Peer-to-peer (P2P)
Hybrid of client-server and P2P
2: Application Layer 7
Client-server architecture
server:
always-on host
permanent IP address
server farms for
scaling
clients:
client/server communicate with server
may be intermittently
connected
may have dynamic IP
addresses
do not communicate
directly with each other
2: Application Layer 8
Pure P2P architecture
no always-on server
arbitrary end systems
directly communicate peer-peer
peers are intermittently
connected and change IP
addresses
example: Gnutella
2: Application Layer 9
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)
2: Application Layer 10
Processes communicating
Process: program running Client process: process
within a host. that initiates
within same host, two
communication
processes communicate Server process: process
using inter-process that waits to be
communication (defined contacted
by OS).
processes in different Note: applications with
hosts communicate by P2P architectures have
exchanging messages client processes &
server processes
2: Application Layer 11
Sockets
process sends/receives
host or host or
server server
messages to/from its
socket controlled by
app developer
socket analogous to door process process
2: Application Layer 13
Addressing processes
to receive messages, identifier includes both
process must have IP address and port
identifier numbers associated with
host device has unique process on host.
32-bit IP address Example port numbers:
Q: does IP address of HTTP server: 80
host on which process Mail server: 25
runs suffice for to send HTTP message
identifying the to gaia.cs.umass.edu web
process? server:
A: No, many IP address: 128.119.245.12
processes can be Port number: 80
running on same host more shortly…
2: Application Layer 14
App-layer protocol defines
Types of messages Public-domain protocols:
exchanged, defined in RFCs
e.g., request, response allows for
Message syntax: interoperability
what fields in messages &
e.g., HTTP, SMTP
how fields are delineated
Proprietary protocols:
Message semantics
meaning of information in
e.g., Skype
fields
Rules for when and how
processes send &
respond to messages
2: Application Layer 15
What transport service does an app need?
Data loss Throughput
some apps (e.g., audio) can some apps (e.g.,
tolerate some loss multimedia) require
other apps (e.g., file minimum amount of
transfer, telnet) require bandwidth to be
100% reliable data
So
transfer called “Quality of Service”
“effective”
other apps (“elastic
Timing apps”) make use of
some apps (e.g., whatever bandwidth
Internet telephony, they get
interactive games)
require low delay to be
“effective”
2: Application Layer 16
Transport service requirements of common apps
2: Application Layer 17
Transport service requirements of common apps
2: Application Layer 18
Transport service requirements of common apps
2: Application Layer 19
Transport service requirements of common apps
2: Application Layer 20
Internet transport protocols services
Application Underlying
Application layer protocol transport protocol
2: Application Layer 22
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
app architectures with TCP
app requirements
2.8 Socket programming
2: Application Layer 23
Web and HTTP
First some jargon
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
2: Application Layer 24
HTTP overview
HTTP: hypertext
transfer protocol
Web’s application layer PC running
protocol Explorer
client/server model
client: browser that
requests, receives, Server
“displays” Web objects running
Apache Web
server: Web server
server
sends objects in
response to requests
Mac running
Navigator
2: Application Layer 25
HTTP overview (continued)
Uses TCP: HTTP is “stateless”
client initiates TCP server maintains no
connection (creates socket) information about
to server, port 80 past client requests
server accepts TCP
connection from client aside
Protocols that maintain
HTTP messages (application- “state” are complex!
layer protocol messages) past history (state) must
exchanged between browser be maintained
(HTTP client) and Web
if server/client crashes,
server (HTTP server)
their views of “state” may
TCP connection closed
be inconsistent, must be
reconciled
2: Application Layer 26
HTTP connections
Nonpersistent HTTP Persistent HTTP
At most one object is Multiple objects can
sent over a TCP be sent over single
connection. TCP connection
between client and
server.
2: Application Layer 27
Nonpersistent HTTP
(contains text,
Suppose user enters URL references to 10
www.someSchool.edu/someDepartment/home.index jpeg images)
time
2: Application Layer 28
Nonpersistent HTTP (cont.)
2: Application Layer 29
Non-Persistent HTTP: Response time
Definition of RTT: time to
send a small packet to
travel from client to
server and back. initiate TCP
connection
Response time: RTT
one RTT to initiate TCP request
file
connection time to
RTT
transmit
one RTT for HTTP file
request and first few file
received
bytes of HTTP response
to return time time
file transmission time
total = 2RTT+transmit time
2: Application Layer 30
Persistent HTTP
2: Application Layer 31
HTTP request message
request line
(GET, POST, GET /somedir/page.html HTTP/1.1
HEAD commands) Host: www.someschool.edu
User-agent: Mozilla/4.0
header Connection: close
lines Accept-language:fr
Carriage return,
line feed (extra carriage return, line feed)
indicates end
of message
2: Application Layer 32
HTTP request message: general format
2: Application Layer 33
Method types
HTTP/1.0 HTTP/1.1
GET GET, POST, HEAD
POST PUT
HEAD uploads file in entity
body to path specified
asks server to leave
in URL field
requested object out of
response DELETE
deletes file specified in
the URL field
2: Application Layer 34
HTTP response message
status line
(protocol
status code HTTP/1.1 200 OK
status phrase) Connection close
Date: Thu, 06 Aug 1998 12:00:15 GMT
Server: Apache/1.3.0 (Unix)
header
Last-Modified: Mon, 22 Jun 1998 …...
lines
Content-Length: 6821
Content-Type: text/html
2: Application Layer 35
HTTP response status codes
In first line in server->client response message.
A few sample codes:
200 OK
request succeeded, requested object later in this message
301 Moved Permanently
requested object moved, new location specified later in
this message (Location:)
400 Bad Request
request message not understood by server
404 Not Found
requested document not found on this server
505 HTTP Version Not Supported
2: Application Layer 36
Trying out HTTP (client side) for yourself
2: Application Layer 37
User-server state: cookies
Example:
Many major Web sites
use cookies Susan always access
Four components: Internet always from PC
1) cookie header line of visits specific e-
HTTP response message commerce site for first
2) cookie header line in time
HTTP request message
3) cookie file kept on when initial HTTP
user’s host, managed by requests arrives at site,
user’s browser
site creates:
4) back-end database at
Web site unique ID
entry in backend
database for ID
2: Application Layer 38
Cookies: keeping “state”
client server
ebay 8734
usual http request msg
Amazon server
cookie file usual http response creates ID
Set-cookie: 1678 1678 for user create
ebay 8734 entry
amazon 1678
usual http request msg
cookie: 1678 cookie- access
specific
one week later: usual http response msg action backend
database
access
ebay 8734 usual http request msg
amazon 1678 cookie: 1678 cookie-
spectific
usual http response msg action
2: Application Layer 39
Cookies (continued)
aside
What cookies can bring: Cookies and privacy:
authorization cookies permit sites to
shopping carts
learn a lot about you
you may supply name
recommendations
and e-mail to sites
user session state
(Web e-mail)
How to keep “state”:
protocol endpoints: maintain state
at sender/receiver over multiple
transactions
cookies: http messages carry state
2: Application Layer 40
Conditional GET
2: Application Layer 43
Caching example
Assumptions origin
average object size = 100,000
servers
bits public
avg. request rate from Internet
institution’s browsers to origin
servers = 15/sec
delay from institutional router
to any origin server and back 1.5 Mbps
to router = 2 sec access link
Consequences
institutional
data rate 1.5Mbps network
utilization on LAN = 15% 10 Mbps LAN
utilization on access link = 100%
total delay = Internet delay +
access delay + LAN delay
= 2 sec + minutes + milliseconds institutional
cache
Why?
2: Application Layer 44
Queueing Delay
institutional
cache
2: Application Layer 46
Caching example (cont)
origin
Install cache servers
suppose hit rate is .4 public
Consequence Internet
40% requests will be
satisfied almost immediately
60% requests satisfied by
origin server 1.5 Mbps
access link
utilization of access link
reduced to 60%, resulting in institutional
negligible delays (say 10 network
10 Mbps LAN
msec)
total avg delay = Internet
delay + access delay + LAN
delay = .6*(2.01) secs +
milliseconds < 1.4 secs institutional
cache
2: Application Layer 47
More about Web caching
Cache acts as both client Why Web caching?
and server
Typically cache is installed
by ISP (university,
company, residential ISP)
2: Application Layer 48
More about Web caching
Cache acts as both client Why Web caching?
and server Reduce response time for
Typically cache is installed client request.
by ISP (university, Reduce traffic on an
company, residential ISP) institution’s access link.
Internet dense with caches
enables “poor” content
providers to effectively
deliver content (but so
does P2P file sharing)
2: Application Layer 49
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP Building a Web server
2.5 DNS by Unix socket
programming
2: Application Layer 50
FTP: the file transfer protocol
目前無法顯示此圖像。
2: Application Layer 51
FTP: separate control, data connections
TCP control connection
FTP client contacts FTP port 21
server at port 21, specifying
TCP as transport protocol
TCP data connection
Client obtains authorization FTP port 20 FTP
over control connection client server
Client browses remote
Server opens a second TCP
directory by sending
commands over control data connection to transfer
connection. another file.
Control connection: “out of
When server receives a
command for a file transfer, band”
the server opens a TCP data FTP server maintains “state”:
connection to client current directory, earlier
After transferring one file, authentication
server closes connection.
2: Application Layer 52
FTP commands, responses
2: Application Layer 53
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP Building a Web server
2.5 DNS by Unix socket
programming
2: Application Layer 54
Electronic Mail outgoing
message queue
user mailbox
user
Three major components: agent
user agents mail
user
server
mail servers agent
simple mail transfer SMTP mail
protocol: SMTP server user
SMTP agent
User Agent
a.k.a. “mail reader” SMTP
mail user
composing, editing, reading agent
server
mail messages
e.g., Eudora, Outlook, elm, user
Netscape Messenger agent
user
outgoing, incoming messages agent
stored on server
2: Application Layer 55
Electronic Mail: mail servers
user
Mail Servers agent
mailbox contains incoming mail
user
messages for user server
agent
message queue of outgoing
SMTP
(to be sent) mail messages mail
server user
SMTP protocol between mail
servers to send email SMTP agent
messages SMTP
client: sending mail mail user
agent
server server
“server”: receiving mail
user
server agent
user
agent
2: Application Layer 56
Electronic Mail: SMTP [RFC 2821]
2: Application Layer 57
Scenario: Alice sends message to Bob
1) Alice uses UA to compose 4) SMTP client sends Alice’s
message and “to” message over the TCP
bob@someschool.edu connection
2) Alice’s UA sends message 5) Bob’s mail server places the
to her mail server; message message in Bob’s mailbox
placed in message queue 6) Bob invokes his user agent
3) Client side of SMTP opens to read message
TCP connection with Bob’s
mail server
1 mail
mail
server user
user server
2 agent
agent 3 6
4 5
2: Application Layer 58
Sample SMTP interaction
S: 220 hamburger.edu
C: HELO crepes.fr
S: 250 Hello crepes.fr, pleased to meet you
C: MAIL FROM: <alice@crepes.fr>
S: 250 alice@crepes.fr... Sender ok
C: RCPT TO: <bob@hamburger.edu>
S: 250 bob@hamburger.edu ... Recipient ok
C: DATA
S: 354 Enter mail, end with "." on a line by itself
C: Do you like ketchup?
C: How about pickles?
C: .
S: 250 Message accepted for delivery
C: QUIT
S: 221 hamburger.edu closing connection
2: Application Layer 59
Try SMTP interaction for yourself:
telnet servername 25
see 220 reply from server
enter HELO, MAIL FROM, RCPT TO, DATA, QUIT
commands
above lets you send email without using email client
(reader)
2: Application Layer 60
SMTP: final words
SMTP uses persistent Comparison with HTTP:
connections
HTTP: pull
SMTP requires message
(header & body) to be in 7- SMTP: push
bit ASCII both have ASCII
SMTP server uses command/response
CRLF.CRLF to determine interaction, status codes
end of message
HTTP: each object
encapsulated in its own
response msg
SMTP: multiple objects
sent in multipart msg
2: Application Layer 61
Mail message format
2: Application Layer 62
Mail access protocols
SMTP SMTP
目前無法顯示此圖像。
access user
user
agent protocol agent
2: Application Layer 63
POP3 protocol S: +OK POP3 server ready
C: user bob
authorization phase S: +OK
C: pass hungry
client commands: S: +OK user successfully logged on
user: declare username
C: list
pass: password S: 1 498
server responses S: 2 912
S: .
+OK
C: retr 1
-ERR S: <message 1 contents>
transaction phase, client: S: .
C: dele 1
list: list message numbers C: retr 2
retr: retrieve message by S: <message 2 contents>
number S: .
C: dele 2
dele: delete
C: quit
quit S: +OK POP3 server signing off
2: Application Layer 64
POP3 (more) and IMAP
More about POP3 IMAP
Previous example uses Keep all messages in
“download and delete” one place: the server
mode. Allows user to
Bob cannot re-read e- organize messages in
mail if he changes folders
client IMAP keeps user state
“Download-and-keep”: across sessions:
copies of messages on names of folders and
different clients mappings between
message IDs and folder
POP3 is stateless
name
across sessions
2: Application Layer 65
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP Building a Web server
2.5 DNS by Unix socket
programming
2: Application Layer 66
DNS: Domain Name System
2: Application Layer 67
DNS name servers
Why not centralize DNS? no server has all name-
single point of failure to-IP address mappings
traffic volume local name servers:
each ISP, company has
distant centralized
local (default) name server
database host DNS query first goes
maintenance to local name server
authoritative name server:
doesn’t scale! for a host: stores that
host’s IP address, name
can perform name/address
translation for that host’s
name
2: Application Layer 68
Distributed, Hierarchical Database
Root DNS Servers
e NASA Mt View, CA
f Internet Software C. Palo Alto,
CA
13 root name
b USC-ISI Marina del Rey, CA servers worldwide
l ICANN Marina del Rey, CA
2: Application Layer 70
TLD and Authoritative Servers
Top-level domain (TLD) servers:
responsible for com, org, net, edu, etc, and all
top-level country domains 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
provider
2: Application Layer 71
Local Name Server
does not strictly belong to hierarchy
each ISP (residential ISP, company,
university) has one.
also called “default name server”
when host makes DNS query, query is sent
to its local DNS server
acts as proxy, forwards query into hierarchy
2: Application Layer 72
DNS name root DNS server
resolution example
2
Host at cis.poly.edu 3
TLD DNS server
wants IP address for 4
gaia.cs.umass.edu 5
gaia.cs.umass.edu
2: Application Layer 73
DNS name
resolution example root DNS server
recursive query: 2 3
puts burden of name 6
7
resolution on
TLD DNS server
contacted name
server
heavy load? local DNS server
dns.poly.edu 5 4
1 8
gaia.cs.umass.edu
2: Application Layer 74
DNS: caching and updating records
once (any) name server learns mapping, it caches
mapping
cache entries timeout (disappear) after some
time
TLD servers typically cached in local name
servers
• Thus root name servers not often visited
update/notify mechanisms under design by IETF
RFC 2136
http://www.ietf.org/html.charters/dnsind-charter.html
2: Application Layer 75
DNS records
DNS: distributed db storing resource records (RR)
RR format: (name, value, type,ttl)
Type=A Type=CNAME
name is hostname name is alias name for some
value is IP address “cannonical” (the real) name
www.ibm.com is really
Type=NS
servereast.backup2.ibm.com
name is domain (e.g.
value is cannonical name
foo.com)
value is IP address of
Type=MX
authoritative name
value is name of mailserver
server for this domain
associated with name
2: Application Layer 76
DNS protocol, messages
DNS protocol : query and reply messages, both with
same message format
msg header
identification: 16 bit #
for query, reply to query
uses same #
flags:
query or reply
recursion desired
recursion available
reply is authoritative
2: Application Layer 77
DNS protocol, messages
RRs in reponse
to query
records for
authoritative servers
additional “helpful”
info that may be used
2: Application Layer 78
Inserting records into DNS
example: new startup “Network Utopia”
register name networkuptopia.com at DNS registrar
(e.g., Network Solutions)
provide names, IP addresses of authoritative name server
(primary and secondary)
registrar inserts two RRs into com TLD server:
2: Application Layer 79
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
app architectures with TCP
app requirements
2.8 Socket programming
2: Application Layer 80
Pure P2P architecture
no always-on server
arbitrary end systems
directly communicate peer-peer
peers are intermittently
connected and change IP
addresses
Three topics:
File distribution
Searching for information
Case Study: Skype
2: Application Layer 81
Comparing Client-server, P2P architectures
Question : How much time distribute file
initially at one server to N other computers?
us: server upload
bandwidth
Server
ui: client/peer i
u1 d1 u2 upload bandwidth
us d2
di: client/peer i
File, size F download bandwidth
dN
Network (with
uN abundant bandwidth)
2: Application Layer 82
Client-server: file distribution time
Server
server sequentially F u1 d1 u2
sends N copies: us d2
Time to distribute F
to N clients using = dcs = max { NF/us, F/min(di) }
i
client/server approach
increases linearly in N
(for large N) 2: Application Layer 83
P2P: file distribution time
Server
server must send one
F u1 d1 u2
copy: F/us time us d2
downloaded (aggregate)
fastest possible upload rate (assuming
all nodes sending file chunks to same
peer): us + ui
i=1,N
2: Application Layer 84
Server-client vs. P2P: example
Client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us
3.5
P2P
Minimum Distribution Time
3
Client-Server
2.5
1.5
0.5
0
0 5 10 15 20 25 30 35
N
2: Application Layer 85
P2P Case Study: BitTorrent
P2P file distribution
obtain list
of peers
trading
chunks
peer
2: Application Layer 86
BitTorrent (1)
file divided into 256KB chunks.
peer joining torrent:
has no chunks, but will accumulate them over time
registers with tracker to get list of peers,
connects to subset of peers (“neighbors”)
while downloading, peer uploads chunks to other
peers.
peers may come and go
once peer has entire file, it may (selfishly) leave or
(altruistically) remain
2: Application Layer 87
BitTorrent (2)
Pulling Chunks Sending Chunks: tit-for-tat
at any given time, Alice sends chunks to
different peers have four neighbors currently
different subsets of sending her chunks at the
file chunks highest rate
periodically, a peer re-evaluate top 4
(Alice) asks each every 10 secs
neighbor for list of every 30 secs: randomly
chunks that they have. select another peer,
Alice issues requests starts sending chunks
for her missing chunks newly chosen peer may
rarest first join top 4
2: Application Layer 88
BitTorrent: Tit-for-tat
(1) Alice “optimistically unchokes” Bob
(2) Alice becomes one of Bob’s top-four providers; Bob reciprocates
(3) Bob becomes one of Alice’s top-four providers
3
15
4
12
5
10
8
Each peer only aware of immediate successor
and predecessor.
“Overlay network”
Circle DHT (2)
1110 0100
1110
1100
1110
1110 0101
Define closest 1110
as closest 1010
successor 1000
Circular DHT with Shortcuts
1 Who’s resp
for key 1110?
3
15
4
12
5
10
8
Each peer keeps track of IP addresses of predecessor,
successor, short cuts.
Reduced from 6 to 2 messages.
Possible to design shortcuts so O(log N) neighbors, O(log
N) messages in query
Peer Churn
1
•To handle peer churn, require
3 each peer to know the IP address
15
of its two successors.
• Each peer periodically pings its
4 two successors to see if they
12 are still alive.
5
10
8
Peer 5 abruptly leaves
Peer 4 detects; makes 8 its immediate successor;
asks 8 who its immediate successor is; makes 8’s
immediate successor its second successor.
What if peer 13 wants to join?
P2P Case study: Skype
Skype clients (SC)
P2P (pc-to-pc, pc-to-
phone, phone-to-pc)
Voice-Over-IP (VoIP) Skype
application login server Supernode
also IM (SN)
proprietary
application-layer
protocol (inferred via
reverse engineering)
hierarchical overlay
2: Application Layer 97
Peers as relays
Problem when both
Alice and Bob are
behind “NATs”.
NAT prevents an outside
peer from initiating a call
to insider peer
Solution:
Using Alice’s and Bob’s
SNs, Relay is chosen
Each peer initiates
session with relay.
Peers can now
communicate through
NATs via relay
2: Application Layer 98
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP Building a Web server
2.5 DNS by Unix socket
programming
2: Application Layer 99
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
controlled by
controlled by process application
application process
developer
developer socket socket
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
More in Chap 3 2: Application Layer 101
Socket programming with TCP
Client must contact server When contacted by client,
server process must first server TCP creates new
be running socket for server process to
server must have created communicate with client
socket (door) that allows server to talk with
welcomes client’s contact multiple clients
source port numbers
Client contacts server by:
used to distinguish
creating client-local TCP
clients (more in Chap3)
socket
specifying IP address, port application viewpoint
number of server process
TCP provides reliable, in-order
When client creates
transfer of bytes (“pipe”)
socket: client TCP
between client and server
establishes connection to
server TCP
2: Application Layer 102
Stream jargon
A stream is a sequence of
characters that flow into
or out of a process.
An input stream is
attached to some input
source for the process, eg,
keyboard or socket.
An output stream is
attached to an output
source, eg, monitor or
socket.
The socket
struct and data handling
System calls
Based on Beej's Guide to Network Programming
2: Application Layer 106
Building a simple Web server
handles one HTTP after creating server,
request you can request file
accepts the request using a browser (eg IE
explorer)
parses header
see text for details
obtains requested file
from server’s file
system
creates HTTP response
message:
header lines + file
sends response to client