Anda di halaman 1dari 105

Chapter 2

Application Layer

CS 6843 Computer Networking

Professor Strauss
Office: LC123
Voice: 718-260-3308

Office Hours: Monday

2: Application Layer 1
2: Application Layer 2
Chapter 2: Application layer
 2.1 Principles of  2.6 P2P applications
network applications  2.7 Socket programming
 2.2 Web and HTTP with UDP
 2.3 FTP  2.8 Socket programming
 2.4 Electronic Mail with TCP
 2.5 DNS

2: Application Layer 3
Chapter 2: Application Layer
Chapter goals:  learn about protocols
 conceptual, by examining popular
implementation application-level
aspects of network protocols
application protocols  HTTP
 transport-layer  FTP
service models  SMTP / POP3 / IMAP
 client-server

paradigm  programming network

 peer-to-peer
paradigm  socket API

2: Application Layer 4
2.0 Some network apps
 e-mail  social networks
 web  voice over IP
 instant messaging  real-time video
 remote login conferencing
 P2P file sharing  grid computing
 multi-user network
 streaming stored video

2: Application Layer 5
2.0 Creating a network app application
data link

write programs that physical

 run on (different) end

 communicate over network
 e.g., web server software
communicates with browser
software application

No need to write software

data link
for network-core devices
 Network-core devices do data link
not run user applications
 applications on end systems
allows for rapid app
development, propagation
2: Application Layer 6
2.1 Application architectures
 Client-server
 Including data centers / cloud computing

 Peer-to-peer (P2P)
 Hybrid of client-server and P2P

2: Application Layer 7
2.1 Client-server architecture
 always-on host
 permanent IP address
 server farms for
client/server  communicate with server
 may be intermittently
 may have dynamic IP
 do not communicate
directly with each other
2: Application Layer 8
2.1 Google Data Centers
 Estimated cost of data center: $600M
 Google spent $2.4B in 2007 on new data
 Each data center uses 50-100 megawatts
of power
2.1 Pure P2P architecture
 no always-on server
 arbitrary end systems
directly communicate peer-peer
 peers are intermittently
connected and change IP

Highly scalable but

difficult to manage

2: Application Layer 10
2.1 Hybrid of client-server and P2P
 voice-over-IP P2P application
 centralized server: finding address of remote
 client-client connection: direct (not through
Instant messaging
 chatting between two users is P2P
 centralized service: client presence
• user registers its IP address with central
server when it comes online
• user contacts central server to find IP
addresses of buddies
2: Application Layer 11
2.1 Processes communicating
Process: program running Client process: process
within a host. that initiates
 within same host, two
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 12
2.1 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

 sending process shoves socket socket

message out door TCP with TCP with
Internet buffers,
 sending process relies on buffers,
variables variables
transport infrastructure
on other side of door which
brings message to socket controlled
by OS
at receiving process

 API: (1) choice of transport protocol; (2) ability to fix

a few parameters (lots more on this later)
2: Application Layer 13
2.1 Addressing processes
 Q: does IP address of
 to receive messages,
host on which process
process must have
runs suffice for
identifying the process?
 host device has unique
 A: No, many processes
32-bit IP address
can be running on
 Exercise: use ipconfig
from command prompt to
 Identifier includes both
get your IP address
IP address and port
numbers associated with
process on host.
 Example port numbers:
 HTTP server: 80
 Mail server: 25
2: Application Layer 14
2.1 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
 Message semantics
Proprietary protocols:
 meaning of information in
fields  e.g., Skype, ppstream
 Rules for when and how
processes send &
respond to messages
2: Application Layer 15
2.1 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 throughput to be
100% reliable data “effective”
 other apps (“elastic apps”)
Timing make use of whatever
 some apps (e.g., throughput they get
Internet telephony,
interactive games) Security
require low delay to be  Encryption, data
“effective” integrity, …

2: Application Layer 16
2.1 Transport service requirements of common apps

Application Data loss Throughput Time Sensitive

file transfer no loss elastic no

e-mail no loss elastic no
Web documents no loss elastic no
real-time audio/video loss-tolerant audio: 5kbps-1Mbps yes, 100’s msec
stored audio/video loss-tolerant same as above yes, few secs
interactive games loss-tolerant few kbps up yes, 100’s msec
instant messaging no loss elastic yes and no

2: Application Layer 17
2.1 Internet transport protocols services

TCP service: UDP service:

 connection-oriented: setup  unreliable data transfer
required between client and between sending and
server processes receiving process
 reliable transport between  does not provide:
sending and receiving process connection setup,
 flow control: sender won’t reliability, flow control,
overwhelm receiver congestion control, timing,
throughput guarantee, or
 congestion control: throttle
sender when network
 does not provide: timing, Q: why bother? Why is
minimum throughput there a UDP?
guarantees, security
2: Application Layer 18
2.1 Internet apps: application, transport protocols

Application Underlying
Application layer protocol transport protocol

e-mail SMTP [RFC 2821] TCP

remote terminal access Telnet [RFC 854] TCP
Web HTTP [RFC 2616] TCP
file transfer FTP [RFC 959] TCP
streaming multimedia HTTP (eg Youtube), TCP or UDP
RTP [RFC 1889]
Internet telephony SIP, RTP, proprietary
(e.g., Skype) typically UDP

2: Application Layer 19
2.2 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:

host name path name

2: Application Layer 20
2.2 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
sends objects in
response to requests
Mac running

2: Application Layer 21
2.2 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

2: Application Layer 22
2.2 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

2: Application Layer 23
2.2 Nonpersistent HTTP
(contains text,
Suppose user enters URL references to 10 jpeg images)

1a. HTTP client initiates TCP

connection to HTTP server
1b. HTTP server at host
(process) at waiting on port 80
for TCP connection at port 80.
“accepts” connection, notifying
2. HTTP client sends HTTP
request message (containing
URL) into TCP connection 3. HTTP server receives request
socket. Message indicates message, forms response
that client wants object message containing requested
someDepartment/home.index object, and sends message
into its socket

2: Application Layer 24
2.2 Nonpersistent HTTP (cont.)
4. HTTP server closes TCP
5. HTTP client receives response
message containing html file,
displays html. Parsing html
file, finds 10 referenced jpeg
time 6. Steps 1-5 repeated for each
of 10 jpeg objects

2: Application Layer 25
2.2 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: RTT
 one RTT to initiate TCP request
connection time to
 one RTT for HTTP file
request and first few file
bytes of HTTP response
to return time time
 file transmission time
total = 2RTT+transmit time
2: Application Layer 26
2.2 Persistent HTTP
Nonpersistent 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

2: Application Layer 27
2.2 HTTP request message
 two types of HTTP messages: request, response
 HTTP request message:
 ASCII (human-readable format)

request line
(GET, POST, GET /somedir/page.html HTTP/1.1
HEAD commands) Host:
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 28
2.2 HTTP request message: general format

2: Application Layer 29
2.2 Uploading form input
Post method:
 Web page often
includes form input URL method:
 Input is uploaded to  Uses GET method
server in entity body  Input is uploaded in
URL field of request

2: Application Layer 30
2.2 Method types
HTTP/1.0 HTTP/1.1
 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 31
2.2 HTTP response message
status line
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)
Last-Modified: Mon, 22 Jun 1998 …...
Content-Length: 6821
Content-Type: text/html

data, e.g., data data data data data ...

HTML file

2: Application Layer 32
2.2 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 33
2.2 Trying out HTTP (client side) for yourself

1. Telnet to your favorite Web server:

telnet 80 Opens TCP connection to port 80
(default HTTP server port) at
Anything typed in sent
to port 80 at

2. Type in a GET HTTP request:

GET /~ross/ HTTP/1.1 By typing this in (hit carriage
Host: return twice), you send
this minimal (but complete)
GET request to HTTP server

3. Look at response message sent by HTTP server!

2: Application Layer 34
2.2 User-server state: cookies
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 35
2.2 Cookies: keeping “state” (cont.)
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
one week later: usual http response msg action backend
ebay 8734 usual http request msg
amazon 1678 cookie: 1678 cookie-
usual http response msg action

2: Application Layer 36
2.2 Cookies (continued)
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
 cookies: http messages carry state

2: Application Layer 37
2.2 Web caches (proxy server)
Goal: satisfy client request without involving origin server

 user sets browser: origin

Web accesses via
cache Proxy
 browser sends all server
HTTP requests to
 object in cache: cache
returns object
 else cache requests
object from origin client
server, then returns server
object to client
2: Application Layer 38
2.2 More about Web caching
 cache acts as both Why Web caching?
client and server  reduce response time
 typically cache is for client request
installed by ISP  reduce traffic on an
(university, company, institution’s access
residential ISP) link.
 Internet dense with
caches: enables “poor”
content providers to
effectively deliver
content (but so does
P2P file sharing)
2: Application Layer 39
2.2 Caching example
Assumptions servers
 average object size =
1,000,000 bits Internet
 avg. request rate from
institution’s browsers to origin
servers = 15/sec
15 Mbps
 delay from institutional router access link
to any origin server and back
to router = 2 sec network
100 Mbps LAN
 utilization on LAN = 15%
 utilization on access link = 100%
 total delay = Internet delay +
access delay + LAN delay
= 2 sec + minutes + milliseconds
2: Application Layer 40
2.2 Caching example (cont)
possible solution servers
 increase bandwidth of access
link to, say, 100 Mbps Internet
 utilization on LAN = 15%
 utilization on access link = 15% 100 Mbps
 Total delay = Internet delay + access link
access delay + LAN delay institutional
= 2 sec + msecs + msecs network
100 Mbps LAN
 often a costly upgrade


2: Application Layer 41
2.2 Caching example (cont)
possible solution: install servers
cache public
 suppose hit rate is 0.4 Internet
 40% requests will be
satisfied almost immediately
15 Mbps
 60% requests satisfied by
access link
origin server
 utilization of access link institutional
reduced to 60%, resulting in network
100 Mbps LAN
negligible delays (say 10
 total avg delay = Internet
delay + access delay + LAN institutional
delay = .6*(2.01) secs +
.4*milliseconds < 1.4 secs cache

2: Application Layer 42
2.2 Conditional GET

 Goal: don’t send object if cache server

cache has up-to-date cached HTTP request msg
 cache: specify date of
cached copy in HTTP request modified
HTTP response
<date> 304 Not Modified
 server: response contains no
object if cached copy is up-
HTTP request msg
to-date: If-modified-since:
HTTP/1.0 304 Not <date> object
Modified modified
HTTP response
HTTP/1.0 200 OK
2: Application Layer 43
2.3 FTP: the file transfer protocol

FTP file transfer

user client server
at host remote file
local file system

 transfer file to/from remote host

 client/server model
 client: side that initiates transfer (either to/from
 server: remote host
 ftp: RFC 959
 ftp server: port 21

2: Application Layer 44
2.3 FTP: separate control, data connections
TCP control connection
 FTP client contacts FTP server port 21
at port 21, TCP is transport
protocol TCP data connection
 client authorized over control FTP port 20 FTP
connection client server
 client browses remote
 server opens another TCP
directory by sending commands
data connection to transfer
over control connection.
another file.
 when server receives file
 control connection: “out of
transfer command, server
opens 2nd TCP connection (for
file) to client  FTP server maintains “state”:
current directory, earlier
 after transferring one file,
server closes data connection.
2: Application Layer 45
2.3 FTP commands, responses
Sample commands: Sample return codes
 sent as ASCII text over  status code and phrase (as
control channel in HTTP)
 USER username  331 Username OK,
 PASS password password required
 125 data connection
 LIST return list of file in
already open;
current directory
transfer starting
 RETR filename retrieves  425 Can’t open data
(gets) file connection
 STOR filename stores  452 Error writing
(puts) file onto remote file

2: Application Layer 46
2.4 Electronic Mail outgoing
message queue
user mailbox
Three major components: agent
 user agents mail
 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
mail messages
 e.g., Eudora, Outlook, elm, user
Mozilla Thunderbird agent
 outgoing, incoming messages agent
stored on server
2: Application Layer 47
2.4 Electronic Mail: mail servers
Mail Servers agent
 mailbox contains incoming mail
messages for user server
 message queue of outgoing
(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
server server
 “server”: receiving mail
server agent

2: Application Layer 48
2.4 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
 commands: ASCII text
 response: status code and phrase

 messages must be in 7-bit ASCII

2: Application Layer 49
2.4 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 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
server user
user server
2 agent
agent 3 6
4 5

2: Application Layer 50
2.4 Sample SMTP interaction
S: 220
S: 250 Hello, pleased to meet you
S: 250 Sender ok
C: RCPT TO: <>
S: 250 ... Recipient ok
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
S: 221 closing connection

2: Application Layer 51
2.4 Try SMTP interaction for yourself:

 telnet servername 25
 see 220 reply from server
above lets you send email without using email client

2: Application Layer 52
2.4 SMTP: final words
 SMTP uses persistent Comparison with HTTP:
 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 53
2.4 Mail message format
SMTP: protocol for
exchanging email msgs header
RFC 822: standard for text
message format:
 header lines, e.g.,

 From:
 Subject:
different from SMTP
 body
 the “message”, ASCII
characters only

2: Application Layer 54
2.4 Mail access protocols
SMTP SMTP access user
agent protocol agent

sender’s mail receiver’s mail

server server

 SMTP: delivery/storage to receiver’s server

 Mail access protocol: retrieval from server
 POP: Post Office Protocol [RFC 1939]
• authorization (agent <-->server) and download
 IMAP: Internet Mail Access Protocol [RFC 1730]
• more features (more complex)
• manipulation of stored msgs on server
 HTTP: gmail, Hotmail, Yahoo! Mail, etc.

2: Application Layer 55
2.4 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 1 contents>
number S: .
C: dele 2
 dele: delete
C: quit
 quit S: +OK POP3 server signing off
2: Application Layer 56
2.4 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
across sessions

2: Application Layer 57
Chapter 2: Application layer
 2.1 Principles of  2.6 P2P applications
network applications  2.7 Socket programming
 2.2 Web and HTTP with UDP
 2.3 FTP  2.8 Socket programming
 2.4 Electronic Mail with TCP
 2.5 DNS

2: Application Layer 58
2.5 DNS: Domain Name System
People: many identifiers: Domain Name System:
 SSN, name, passport #  distributed database
Internet hosts, routers: implemented in hierarchy of
many name servers
 IP address (32 bit) -
 application-layer protocol
used for addressing
host, routers, name servers to
communicate to resolve names
 “name”, e.g., (address/name translation) - used by
 note: core Internet
function, implemented as
Q: map between IP application-layer protocol
addresses and name ?  complexity at network’s

2: Application Layer 59
2.5 DNS
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

2: Application Layer 60
2.5 Distributed, Hierarchical Database
Root DNS Servers

TLD namesrvrs
com DNS servers org DNS servers edu DNS servers
DNS servers DNS serversDNS servers
DNS servers DNS servers

Client wants IP for; 1st approx:

 client queries a root server to find com DNS server
 client queries com DNS server to get
DNS server
 client queries DNS server to get IP
address for
2: Application Layer 61
2.5 DNS: Root name servers
 Provides top-level domain (TLD) name server for top-level domain.
 TLDs: .com, .edu, .org, .fr, etc.
 Maps “.com” to name server (host name and IP address) for .com

a Verisign, Dulles, VA
c Cogent, Herndon, VA (also LA)
d U Maryland College Park, MD k RIPE London (also 16 other locations)
g US DoD Vienna, VA
h ARL Aberdeen, MD i Autonomica, Stockholm (plus
j Verisign, ( 21 locations) 28 other locations)
e NASA Mt View, CA m WIDE Tokyo (also Seoul,
f Internet Software C. Palo Alto, Paris, SF)
CA (and 36 other locations)

13 root name
servers worldwide
b USC-ISI Marina del Rey, CA
l ICANN Los Angeles, CA

2: Application Layer 62
2.5 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

2: Application Layer 63
2.5 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
 Organizations local name server and
authoritative name typically combined on
one machine (BIND software).
2: Application Layer 64
2.5 DNS name root DNS server

resolution example
 Host at 3
TLD DNS server
wants IP address for 4 5

iterated query: local DNS server
 contacted server 7 6
replies with name of 1 8
server to contact
authoritative DNS server
 “I don’t know this
name, but ask this requesting host

2: Application Layer 65
2.5 DNS name
root DNS server
resolution example
recursive query: 2 3
 puts burden of name 6
resolution on
TLD DNS server
contacted name
 heavy load? local DNS server 5 4

1 8

authoritative DNS server
requesting host
2: Application Layer 66
2.5 DNS: caching and updating records
 once (any) name server learns mapping, it caches
 cache entries timeout (disappear) after some
 TLD servers typically cached in local name
• Thus root name servers not often visited
 update/notify mechanisms under design by IETF
 RFC 2136

2: Application Layer 67
2.5 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 “canonical” (the real) name is really
 Type=NS
 name is domain (e.g.
 value is canonical name
 value is hostname of
 Type=MX
authoritative name
 value is name of mailserver
server for this domain
associated with name

2: Application Layer 68
2.5 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 69
2.5 DNS protocol, messages

Name, type fields

for a query

RRs in response
to query

records for
authoritative servers

additional “helpful”
info that may be used

2: Application Layer 70
2.5 Inserting records into DNS
 example: new startup “Network Utopia”
 register name 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:

(,, NS)

(,, A)

 create authoritative server Type A record for; Type MX record for
 How do people get IP address of your Web site?

2: Application Layer 71
2.6 Pure P2P architecture
 no always-on server
 arbitrary end systems
directly communicate peer-peer
 peers are intermittently
connected and change IP

 Three topics:
 File distribution
 Searching for information
 Case Study: Skype

2: Application Layer 72
2.6 File Distribution: Server-Client vs P2P
Question : How much time to distribute file
from one server to N peers?
us: server upload
ui: peer i upload
u1 d1 u2 bandwidth
us d2
di: peer i download
File, size F bandwidth
Network (with
uN abundant bandwidth)

2: Application Layer 73
2.6 File distribution time: server-client
 server sequentially F u1 d1 u2
sends N copies: us d2

 NF/us time dN Network (with

abundant bandwidth)
 client i takes F/di uN
time to download

Time to distribute F
to N clients using = dcs = max { NF/us, F/min(di) }
client/server approach
increases linearly in N
(for large N) 2: Application Layer 74
2.6 File distribution time: P2P
 server must send one
F u1 d1 u2
copy: F/us time us d2

 client i takes F/di time

Network (with
to download
abundant bandwidth)
 NF bits must be

downloaded (aggregate)
 fastest possible upload rate: us + Σui

dP2P = max { F/us, F/min(di) , NF/(us + Σui) }

2: Application Layer 75
2.6 Server-client vs. P2P: example
Client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us

Minimum Distribution Time




0 5 10 15 20 25 30 35

2: Application Layer 76
2.6 File distribution: BitTorrent
 P2P file distribution

tracker: tracks peers torrent: group of

participating in torrent peers exchanging
chunks of a file

obtain list
of peers


2: Application Layer 77
2.6 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 may come and go
 once peer has entire file, it may (selfishly) leave or
(altruistically) remain
2: Application Layer 78
2.6 BitTorrent (2) Sending Chunks: tit-for-tat
 Alice sends chunks to four
Pulling Chunks
neighbors currently
 at any given time,
sending her chunks at the
different peers have highest rate
different subsets of
file chunks  re-evaluate top 4 every
10 secs
 periodically, a peer
 every 30 secs: randomly
(Alice) asks each
neighbor for list of select another peer,
chunks that they have. starts sending chunks
 newly chosen peer may
 Alice sends requests
for her missing chunks join top 4
 “optimistically unchoke”
 rarest first

2: Application Layer 79
2.6 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

With higher upload rate,

can find better trading
partners & get file faster!
2: Application Layer 80
2.6 Distributed Hash Table (DHT)
 DHT = distributed P2P database
 Database has (key, value) pairs;
 key: ss number; value: human name
 key: content type; value: IP address

 Peers query DB with key

 DB returns values that match the key

 Peers can also insert (key, value) peers

2.6 DHT Identifiers
 Assign integer identifier to each peer in range
 Each identifier can be represented by n bits.
 Require each key to be an integer in same range.
 To get integer keys, hash original key.
 eg, key = h(“Led Zeppelin IV”)
 This is why they call it a distributed “hash” table
2.6 How to assign keys to peers?
 Central issue:
 Assigning (key, value) pairs to peers.

 Rule: assign key to the peer that has the

closest ID.
 Convention in lecture: closest is the
immediate successor of the key.
 Ex: n=4; peers: 1,3,4,5,8,10,12,14;
 key = 13, then successor peer = 14
 key = 15, then successor peer = 1
2.6 Circular DHT (1)

15 3

 Each peer only aware of immediate successor
and predecessor.
 “Overlay network”
2.6 Circle DHT (2)

O(N) messages 0001 Who’s resp

on avg to resolve for key 1110 ?
I am
query, when there 0011
are N peers

1110 0100

1110 0101
Define closest 1110

as closest 1010
successor 1000
2.6 Circular DHT with Shortcuts
1 Who’s resp
for key 1110?

 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
2.6 Peer1 Churn
•To handle peer churn, require

15 3 each peer to know the IP address

of its two successors.
• Each peer periodically pings its
4 two successors to see if they
12 are still alive.
 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?
2.6 P2P Case study: Skype
Skype clients (SC)
 inherently P2P: pairs
of users communicate.
 proprietary Skype
application-layer login server Supernode
protocol (inferred via (SN)
reverse engineering)
 hierarchical overlay
with SNs
 Index maps usernames
to IP addresses;
distributed over SNs

2: Application Layer 88
2.6 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 89
2.7 Socket programming
Goal: learn how to build client/server application that
communicate using sockets

Socket API socket

 introduced in BSD4.1 UNIX,
1981 A application-created,
 explicitly created, used, OS-controlled interface
released by apps (a “door”) into which
application process can
 client/server paradigm
both send and
 two types of transport receive messages to/from
service via socket API: another application
 UDP process

2: Application Layer 90
2.7 Socket programming basics
 Server must be  Socket is locally
running before identified with a port
client can send number
anything to it.  Analogous to the apt #
 Server must have a in a building
socket (door)  Client needs to know
through which it server IP address and
receives and sends socket port number.
 Similarly client
needs a socket
2: Application Layer 91
2.7 Socket programming with UDP
UDP: no “connection” between
client and server
 no handshaking
 sender explicitly attaches application viewpoint
IP address and port of
destination to each segment UDP provides unreliable transfer
of groups of bytes (“datagrams”)
 OS attaches IP address and
between client and server
port of sending socket to
each segment
 Server can extract IP
address, port of sender Note: the official terminology
from received segment for a UDP packet is “datagram”.
In this class, we instead use “UDP

2: Application Layer 92
2.7 Running example
 Client:
 User types line of text
 Client program sends line to server

 Server:
 Server receives line of text
 Capitalizes all the letters
 Sends modified line to client

 Client:
 Receives line of text
 Displays

2: Application Layer 93
2.7 Client/server socket interaction: UDP
Server (running on hostid) Client

create socket, create socket,

port= x. clientSocket =
serverSocket = socket(AF_INET, SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
Read UDP segment clientSocket
from serverSocket

write reply to
specifying read datagram from
client address, clientSocket
port number close

2: Application Layer 94
2.7 UDP Client
from socket import * address format

serverIP = “put server IP address here”

create UDP socket serverPort = 12000
Prompts user with
“Enter sent:”, reads
clientSocket = socket(AF_INET, SOCK_DGRAM)
line from keyboard.
message = raw_input(“Enter sentence:”)
Create UDP segment
and give to socket clientSocket.sendto(message,( serverIP, serverPort))

modifiedMessage, address = clientSocket.recvfrom(1024)

Read UDP segment
from socket, hold in print modifiedMessage
buffer of size 1024,
extract modified clientSocket.close()
sentence and
address 2: Application Layer 95
2.7 UDP server
from socket import *
serverPort = 12000
assigns the local IP
address and the port
number to the socket serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind((‘’, serverPort))
print "The server is ready to connect“
return address
in UDP segment
while True:
message, address = serverSocket.recvfrom(1024)
message = message.upper()
print message
serverSocket.sendto(message, address)

2: Application Layer 96
2.7 UDP observations & questions
 Both client server use socket type: SOCK_DGRAM
 Dest IP and port are explicitly attached to
 What would happen if change both clientSocket
and serverSocket to “mySocket”?
 Can the client send a segment to server without
knowing the server’s IP address and/or port
 Can multiple clients use the server?

2: Application Layer 97
2.8 Socket-programming using TCP

TCP service: reliable transfer of bytes from one

process to another

controlled by
controlled by process application
application process
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

2: Application Layer 98
2.8 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 Chap 3)
 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 99
2.8 Client/server socket interaction: TCP
Server (running on serverIP) Client
create socket,
port=x, for
incoming request:
serverSocket =
TCP create socket,
wait for incoming
connection request connection setup connect to serverIP, port=x
connectionSocket = clientSocket =socket()

send request using

read request from clientSocket

write reply to
connectionSocket read reply from
connectionSocket close
2: Application Layer 100
2.8 TCP client
from socket import *

serverIP='put server IP address here'

Create TCP socket
clientSocket=socket(AF_INET, SOCK_STREAM)
Create TCP conx
clientSocket.connect((serverIP, serverPort))
sentence = raw_input(‘Enter sentence:‘)
Send sentence into
TCP conx. No need clientSocket.send(sentence)
to specify IP or port.
send instead of sendto modifiedSentence=clientSocket.recv(1024)

print 'From Server:', modifiedSentence

recv instead of clientSocket.close()
2: Application Layer 101
2.8 TCP server from socket import *
required command: serverPort=6789
indicates server should serverSocket=socket(AF_INET,SOCK_STREAM)
accept at most 1 conx serverSocket.bind(('',serverPort))

accept() method returns

while True:
new socket object called
connectionSocket, addr = serverSocket.accept()
connectionSocket &
sentence = connectionSocket.recv(1024)
(IP port) called addr
capitalizedSentence = sentence.upper()
recv() method takes connectionSocket.send(capitalizedSentence)
bytes received from
TCP conx, puts them connectionSocket.close()
in string sentence
2: Application Layer 102
2.8 TCP observations & questions
 Both client and server use socket type
 But socket on server side is made a “server
socket” with serverSocket.listen(1)
 When client knocks on serverSocket’s “door,”
server creates connectionSocket and completes
TCP conx.
 Dest IP and port are not explicitly attached to
 Can multiple clients use the server?

2: Application Layer 103

Chapter 2: Summary
our study of network apps now complete!
 application architectures  specific protocols:
 client-server  HTTP

 P2P  FTP

 hybrid  SMTP, POP, IMAP

 application service
 P2P: BitTorrent, Skype
 reliability, bandwidth,  socket programming
 Internet transport
service model
 connection-oriented,
reliable: TCP
 unreliable, datagrams: UDP
2: Application Layer 104
Chapter 2: Summary
Most importantly: learned about protocols

 typical request/reply Important themes:

message exchange:  control vs. data msgs
client requests info or
in-band, out-of-band

 server responds with  centralized vs.
data, status code decentralized
 message formats:  stateless vs. stateful
 headers: fields giving  reliable vs. unreliable
info about data msg transfer
 data: info being
communicated  “complexity at network
2: Application Layer 105