You are on page 1of 104

Addis Ababa University

Department of Computer Science

Advanced Computer
Networking
(CS 723)
Chapter 2: Application Layer
(Course Material mainly adapted from:
Kurose, Tannenbaum and/or Forouzan)
Some network apps
e-mail
web
instant messaging
remote login
P2P file sharing
multi-user network games
streaming stored video clips
voice over IP
real-time video conferencing
grid computing

2: Application Layer 2
Creating a network app application
transport
network
data link

Write programs that physical

run on (different) end


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

No need to write software


data link
application
physical
transport

for network-core devices network


data link
physical
Network-core devices do
not run user applications

2: Application Layer 3
Chapter 2: Application layer
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
2.9 Building a Web server

2: Application Layer 4
Application architectures
Client-server
Peer-to-peer (P2P)
Hybrid of client-server and P2P

2: Application Layer 5
Client-server architecture
server:
always-on host
permanent IP address
server farms for scaling

clients:
communicate with server
may be intermittently
client/server connected
may have dynamic IP
addresses
do not communicate directly
with each other

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

Highly scalable but


difficult to manage

2: Application Layer 7
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
2: Application Layer 8
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 9
Sockets
host or host or
process sends/receives server server
messages to/from its
socket controlled by
app developer
socket analogous to door process process

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

2: Application Layer 10
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 process? to a web server with IP
A: No, many
address:217.110.045.12,
processes can be we use:
running on same host
217.110.045.12:80
2: Application Layer 11
App-layer protocol defines
Types of messages Public-domain protocols:
exchanged, defined in RFCs
e.g., request, response e.g. HTTP (RFC 2616)
Message syntax: allows for
what fields in messages &
interoperability
how fields are delineated e.g., HTTP, SMTP
Message semantics
Proprietary protocols:
meaning of information in
e.g., Skype
fields
Rules for when and how
processes send &
respond to messages
2: Application Layer 12
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
transfer other apps (elastic apps)
Timing make use of whatever
some apps (e.g., throughput they get
Internet telephony, Security
interactive games)
require low delay to be Encryption, data integrity,
effective

2: Application Layer 13
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, 100s msec
video:10kbps-5Mbps
stored audio/video loss-tolerant same as above yes, few secs
interactive games loss-tolerant few kbps up yes, 100s msec
instant messaging no loss elastic yes and no

2: Application Layer 14
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 wont reliability, flow control,
overwhelm receiver congestion control, timing,
congestion control: throttle throughput guarantee, or
sender when network security
overloaded
does not provide: timing,
minimum throughput
guarantees, security

2: Application Layer 15
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 16
Chapter 2: Application layer
2.1 Principles of 2.6 P2P applications
network applications 2.7 Socket programming
app architectures with TCP
app requirements 2.8 Socket programming
2.2 Web and HTTP with UDP
2.4 Electronic Mail
SMTP, POP3, IMAP

2.5 DNS

2: Application Layer 17
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.aau.edu.et/csDept/pic.jpg

host name path name

2: Application Layer 18
HTTP overview

HTTP: hypertext
transfer protocol HT
TP
r
equ
Webs application layer PC running HT est
TP
protocol Explorer res
pon
se
client/server model
client: browser that
est
u
requests, receives, P req se Server
T o n
displays Web objects HT r es
p running
T P Apache Web
server: Web server HT
server
sends objects in
response to requests Mac running
Navigator

2: Application Layer 19
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 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 20
HTTP connections
Non-persistent HTTP
At most one object is sent over a TCP
connection.

Persistent HTTP
Multiple objects can be sent over single TCP
connection between client and server.

2: Application Layer 21
Non-persistent HTTP
(contains text,
Suppose user enters URL www.aau.edu.et/csDepartment/home.index
references to 10
jpeg images)

1a. HTTP client initiates TCP


connection to HTTP server
1b. HTTP server at host
(process) at
www.aau.edu.et waiting for
www.aau.edu.et on port 80 TCP connection at port 80.
accepts connection, notifying
client
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
csDepartment/home.index object, and sends message
into its socket

time
2: Application Layer 22
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 referenced jpeg
objects
time 6. Steps 1-5 repeated for each
of 10 jpeg objects

2: Application Layer 23
Non-Persistent HTTP: Response time
Definition of RTT: time for a
small packet to travel
from client to server and
back. initiate TCP
connection
Response time:
RTT
one RTT to initiate TCP
request
connection file
time to
one RTT for HTTP request RTT
transmit
and first few bytes of file
file
HTTP response to return received
file transmission time
total = 2RTT+transmit time time time

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

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

GET/POST/HEAD/PUT methods/commands:
request line
GET /somedir/page.html HTTP/1.1
Host: www.someschool.edu
User-agent: Mozilla/4.0
header
Connection: close
lines
Accept-language:fr
Carriage return, line
feed - indicates end (extra carriage return, line feed)
of message
2: Application Layer 26
HTTP request message: general format

2: Application Layer 27
Uploading form input
Post method:
Web page often includes form input
Input is uploaded to server in entity body
www.somesite.com/animalsearch?monkeys&banana

URL method:
Uses GET method
Input is uploaded in URL field of request line:

2: Application Layer 28
Method types
HTTP/1.0
GET
POST
HEAD
Similar to get but asks server to leave requested object
out of response
HTTP/1.1
GET, POST, HEAD
PUT
uploads file in entity body to path specified in URL field
web publishing
DELETE
deletes file specified in the URL field
2: Application Layer 29
HTTP response message
status line (protocol,
status code - status
phrase) HTTP/1.1 200 OK
Connection: close
Date: Thu, 06 Aug 1998 12:00:15 GMT
header Server: Apache/1.3.0 (Unix)
lines Last-Modified: Mon, 22 Jun 1998 ...
Content-Length: 6821
Content-Type: text/html

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


requested
HTML file

2: Application Layer 30
HTTP response message: general format

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

1. Telnet to your favorite Web server:


telnet www.aau.edu.et 80 Opens TCP connection to port 80
(default HTTP server port) at cis.poly.edu.
Anything typed is sent
to port 80 at cis.poly.edu

2. Type in a GET HTTP request:


GET /~ross/ HTTP/1.1 By typing this in (hit carriage
Host: www.aau.edu.et 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 33
User-server state: cookies
Many major Web sites use cookies
Four components:
1) cookie header line in HTTP response message
2) cookie header line in HTTP request message
3) cookie file kept on users host, managed by users browser
4) back-end database at Web site
Example:
Alice always access Internet from PC
visits specific e-commerce site for first time
when initial HTTP requests arrives at site, site
creates:
unique ID
entry in backend database for ID
2: Application Layer 34
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
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 35
Cookies (continued)
What cookies can bring:
authorization
shopping carts
recommendations
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

Cookies and privacy:


cookies permit sites to learn a lot about you
you may supply name and e-mail to sites
2: Application Layer 36
Web caches (proxy server)
Goal: satisfy client request without involving origin server

user sets browser: Web origin


server
accesses via cache
browser sends all HTTP Proxy
HT
requests to cache TP
req server q u est
H u P re
T
client TP e st TT o n se
object in cache: cache H p
res res

pon P
returns object se H TT
HT
e st TP
else cache requests qu H
P re ns e TT re
qu
object from origin server, T p o P es
HT r e s re
s
t
then returns object to TP p on
HT se
client
client
origin
server

2: Application Layer 37
More about Web caching
cache acts as both client and server
typically cache is installed by ISP (university,
company, residential ISP)

Why Web caching?


reduce response time for client request
reduce traffic on an institutions access link.
Internet dense with caches: enables poor
content providers to effectively deliver content

2: Application Layer 38
Caching scenario
Assumptions origin
average object size = 1Mbps servers
average request rate from
public
institutions browsers to origin Internet
servers = 15/sec
delay from the router on the
Internet side of the access link 1.5 Mbps
to any origin server and back is access link
= 2 sec (Internet delay)
institutional
Consequences network
100 Mbps LAN
total delay = Internet delay +
access delay + LAN delay
= 2 sec + >20 sec+ milliseconds

2: Application Layer 39
Caching scenario (cont)
origin
possible solution (expensive) servers
increase bandwidth of access
public
link to, say, 10 Mbps Internet
Consequence
Total delay = Internet delay +
access delay + LAN delay 15 Mbps
= 2 sec + 2 sec + msecs access link
often a costly upgrade institutional
network
100 Mbps LAN

2: Application Layer 40
Caching scenario (cont)
origin
servers
possible solution: install cache
suppose hit rate is 0.5 (up to 0.7) public
Internet

consequence
50% requests satisfied almost
immediately 1.5 Mbps
50% requests satisfied by origin access link
server institutional
utilization of access link reduced network
100 Mbps LAN
to 50%, resulting in lower delay
rate

institutional
Cashes may not have up to date cache
version of the resource!

2: Application Layer 41
Conditional GET

Goal: dont send object if cache server


cache has up-to-date cached HTTP request msg
version If-modified-since:
object
cache: specify date of cached <date>
not
copy in HTTP request modified
HTTP response
If-modified-since: <date> HTTP/1.0
server: response contains no 304 Not Modified
object if cached copy is up-to-
date: HTTP request msg
HTTP/1.0 304 Not Modified If-modified-since:
<date> object
modified
HTTP response
HTTP/1.0 200 OK
<data>
2: Application Layer 42
Chapter 2: Application layer
2.1 Principles of 2.6 P2P applications
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 2.9 Building a Web
2.5 DNS server

2: Application Layer 43
FTP: the file transfer protocol

FTP file transfer


FTP FTP
user client server
interface
user
at host remote file
local file system
system

transfer file to/from remote host


client/server model
client: side that initiates transfer (either to/from
remote)
server: remote host

ftp: RFC 959


ftp server: port 21

2: Application Layer 44
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
transfer command, server
FTP server maintains state:
opens 2nd TCP connection (for
file) to client, port 20 current directory, earlier
authentication
after transferring one file,
server closes data connection.
2: Application Layer 45
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
LIST return list of file in 125 data connection
current directory already open;
transfer starting
RETR filename retrieves
425 Cant open data
(gets) file connection
STOR filename stores 452 Error writing
(puts) file onto remote file
host

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

2: Application Layer 47
Electronic Mail outgoing
message queue
user mailbox
Three major components: user
agent
user agents
mail
mail servers user
server
agent
simple mail transfer protocol:
SMTP SMTP mail
server user
User Agent SMTP agent
mail reader
composing, editing, reading mail SMTP
mail user
messages server agent
e.g., Eudora, Outlook, Mozilla
Thunderbird user
outgoing, incoming messages agent
stored on server user
agent

2: Application Layer 48
Electronic Mail: mail servers
user
Mail Servers agent
mailbox contains incoming mail
user
messages for user server
agent
message queue of outgoing
SMTP mail
(to be sent) mail messages
server user
SMTP protocol between mail
servers to send email
SMTP agent

messages SMTP
client: sending mail mail user
server agent
server
server: receiving mail
user
server agent
user
agent

2: Application Layer 49
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 50
Scenario: Alice sends message to Bob
1) Alice uses UA to compose 4) SMTP client sends Alices
message to message over the TCP
bob@someschool.edu connection
2) Alices UA sends message 5) Bobs mail server places the
to her mail server; message message in Bobs mailbox
placed in message queue 6) Bob invokes his user agent
3) Client side of SMTP opens to read message
TCP connection with Bobs
mail server (port 25)

1 mail
mail
server user
user server
2 agent
agent 3 6
4 5

2: Application Layer 51
SMTP by interaction
telnet servername 25
see reply from server
enter HELO, MAIL FROM, RCPT TO, DATA, QUIT
commands above lets you send email without using
email client (reader)
SMTP principles
SMTP uses persistent connections
SMTP requires message (header & body) to be in 7-
bit ASCII
SMTP server uses CRLF.CRLF to determine end of
message
SMTP is push (contrast with pull in HTTP)
2: Application Layer 52
Mail message format

SMTP: protocol for


exchanging email msgs header
blank
RFC 822: standard for text
line
message format:
header lines, e.g.,
To: body
From:
Subject:
body
the message, ASCII
characters only

2: Application Layer 53
Message format: multimedia extensions
MIME: multimedia mail extension, RFC 2045, 2056
additional lines in msg header declare MIME content
type

From: alice@crepes.fr
MIME version To: bob@hamburger.edu
Subject: Picture of yummy crepe.
method used MIME-Version: 1.0
to encode data Content-Transfer-Encoding: base64
Content-Type: image/jpeg
multimedia data
type, subtype, base64 encoded data .....
parameter declaration .........................
......base64 encoded data
encoded data

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

senders mail receivers mail


server server
SMTP: delivery/storage to receivers 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
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
+OK S: .
-ERR C: retr 1
S: <message 1 contents>
transaction phase, client: S: .
list: list message numbers C: dele 1
retr: retrieve message by C: retr 2
number S: <message 1 contents>
S: .
dele: delete
C: dele 2
quit C: quit
S: +OK POP3 server signing off
2: Application Layer 56
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
POP3 is stateless message IDs and folder
across sessions name

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 TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
SMTP, POP3, IMAP 2.9 Building a Web
2.5 DNS server

2: Application Layer 58
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
datagrams
communicate to resolve names
name, e.g., (address/name translation)
ww.yahoo.com - used by note: core Internet
humans
function, implemented as
Q: map between IP application-layer protocol
addresses and name ? complexity at networks
edge

2: Application Layer 59
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 servers:
doesnt scale!
set of IP addresses for
one canonical name

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

com DNS servers org DNS servers edu DNS servers

pbs.org poly.edu umass.edu


yahoo.com amazon.com
DNS servers DNS serversDNS servers
DNS servers DNS servers

Client wants IP for www.amazon.com; 1st approx:


client queries a root server to find com DNS server
client queries com DNS server to get amazon.com
DNS server
client queries amazon.com DNS server to get IP
address for www.amazon.com
2: Application Layer 61
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 name server
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)
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
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, et ...
Network Solutions maintains servers for com TLD
Educause for edu TLD

Authoritative DNS servers:


organizations DNS servers, providing
authoritative hostname to IP mappings for
organizations servers (e.g., Web, mail).
can be maintained by organization or service
provider

2: Application Layer 63
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 64
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 65
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
Type=NS www.ibm.com in reality is
name is domain (e.g. foo.com) servereast.backup2.ibm.com
value is hostname of
value is canonical name
authoritative name server
for this domain Type=MX
value is name of mailserver
associated with name

2: Application Layer 66
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 67
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

Read more on how we create and register a DNS!


2: Application Layer 68
Chapter 2: Application layer
2.1 Principles of 2.6 P2P applications
network applications 2.7 Socket programming
app architectures with TCP
app requirements 2.8 Socket programming
2.2 Web and HTTP with UDP
2.4 Electronic Mail
SMTP, POP3, IMAP

2.5 DNS

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

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

trading
chunks

peer

2: Application Layer 71
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 72
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
re-evaluate top 4 every
file chunks
10 secs
periodically, a peer
every 30 secs: randomly
(Alice) asks each
select another peer,
neighbor for list of
starts sending chunks
chunks that they have.
newly chosen peer may
Alice sends requests
join top 4
for her missing chunks
optimistically unchoke
rarest first

2: Application Layer 73
BitTorrent: Tit-for-tat
(1) Alice optimistically unchokes Bob
(2) Alice becomes one of Bobs top-four providers; Bob reciprocates
(3) Bob becomes one of Alices top-four providers

With higher upload rate,


can find better trading
partners & get file faster!
2: Application Layer 74
P2P: searching for information
Index in P2P system: maps information to peer location
(location = IP address & port number)
. Instant messaging
File sharing (eg e-mule)
Index dynamically Index maps user
tracks the locations of names to locations.
files that peers share. When user starts IM
Peers need to tell application, it needs to
index what they have. inform index of its
Peers search index to location
determine where files Peers search index to
can be found determine IP address
of user.

2: Application Layer 75
P2P: centralized index
Bob
original Napster design centralized
directory server
1) when peer connects, it 1
informs central server: peers
1
IP address
content
1 3
2) Alice queries for Hey
2 1
Jude
3) Alice requests file from
Bob
Alice

2: Application Layer 76
P2P: problems with centralized directory

single point of failure


performance bottleneck
copyright infringement: target of lawsuit is
obvious

file transfer is decentralized, but locating content


is highly centralized

2: Application Layer 77
Query flooding
fully distributed overlay network: graph
no central server edge between peer X
used by Gnutella and Y if theres a TCP
Each peer indexes the connection
files it makes available all active peers and
for sharing (and no edges form overlay net
other files) edge: virtual (not
physical) link
given peer typically
connected with < 10
overlay neighbors

2: Application Layer 78
Query flooding
File transfer:
Query message
HTTP
sent over existing TCP
connections
Query
peers forward
QueryHit
Query message
ry Qu
QueryHit e ery
Qu H it
sent over ery
Qu
reverse
Query
path
QueryHit

Qu
er
y

2: Application Layer 79
Gnutella: Peer joining
1. joining peer Alice must find another peer in
Gnutella network: use list of candidate peers
2. Alice sequentially attempts TCP connections with
candidate peers until connection setup with Bob
3. Flooding: Alice sends Ping message to Bob; Bob
forwards Ping message to his overlay neighbors
(who then forward to their neighbors.)
peers receiving Ping message respond to Alice
with Pong message
4. Alice receives many Pong messages, and can then
setup additional TCP connections

2: Application Layer 80
Hierarchical Overlay
between centralized
index, query flooding
approaches
each peer is either a
super node or assigned to
a super node
TCP connection between
peer and its super node.
TCP connections between
some pairs of super nodes. o rd in a ry p e e r

Super node tracks g r o u p - le a d e r p e e r

content in its children n e ig h o r in g r e la tio n s h ip s


in o v e r la y n e tw o r k

2: Application Layer 81
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 82
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 Alices and Bobs
SNs, Relay is chosen
Each peer initiates
session with relay.
Peers can now
communicate through
NATs via relay

2: Application Layer 83
Chapter 2: Application layer
2.1 Principles of 2.6 P2P applications
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
2.5 DNS

2: Application Layer 84
Socket programming
Goal: learn how to build client/server application that
communicate using sockets

Socket API socket


introduced in BSD4.1 UNIX,
a host-local,
1981
application-created,
explicitly created, used, OS-controlled interface
released by apps (a door) into which
client/server paradigm application process can
two types of transport both send and
service via socket API: receive messages to/from
reliable, byte stream- another application
process
oriented
unreliable datagram

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

2: Application Layer 86
Socket programming with TCP
Client must contact server
server process must first be running
server must have created socket (door) that welcomes clients
contact
Client contacts server by:
creating client-local TCP socket
specifying IP address, port number of server process
When client creates socket: client TCP establishes connection to
server TCP
When contacted by client, server TCP creates new socket for
server process to communicate with client
allows server to talk with multiple clients
source port numbers used to distinguish clients

2: Application Layer 87
Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()

TCP create socket,


wait for incoming
connection request connection setup connect to hostid, port=x
connectionSocket = clientSocket =
welcomeSocket.accept() Socket()

send request using


read request from clientSocket
connectionSocket

write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
2: Application Layer 88
Stream jargon
keyboard monitor

A stream is a sequence of
characters that flow into
or out of a process.

inFromUser
input
stream
An input stream is Client
Process
attached to some input process
source for the process,
e.g., keyboard or socket.
An output stream is
attached to an output

inFromServer
outToServer
output input
source, e.g., monitor or stream stream

socket.
client
clientSocket
TCP
socket TCP
socket

to network from network

2: Application Layer 89
Socket programming with TCP
Example client-server app:
1) client reads line from standard
input (inFromUser stream) ,
sends to server via socket
(outToServer stream)

2) server reads line from socket

3) server converts line to


uppercase, sends back to client

4) client reads, prints modified


line from socket
(inFromServer stream)

2: Application Layer 90
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {

public static void main(String argv[]) throws Exception


{
String sentence;
String modifiedSentence;
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket, Socket clientSocket = new Socket("hostname", 6789);
connect to server
Create DataOutputStream outToServer =
output stream new DataOutputStream(clientSocket.getOutputStream());
attached to socket
2: Application Layer 91
Example: Java client (TCP), cont.

Create BufferedReader inFromServer =


input stream new BufferedReader(new
attached to socket InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');

Read line modifiedSentence = inFromServer.readLine();


from server
System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close();

}
}
2: Application Layer 92
Example: Java server (TCP)
import java.io.*;
import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception


{
String clientSentence;
Create String capitalizedSentence;
welcoming socket
ServerSocket welcomeSocket = new ServerSocket(6789);
at port 6789
while(true) {
Wait, on welcoming
socket for contact Socket connectionSocket = welcomeSocket.accept();
by client
BufferedReader inFromClient =
Create input new BufferedReader(new
stream, attached InputStreamReader(connectionSocket.getInputStream()));
to socket

2: Application Layer 93
Example: Java server (TCP), cont

Create output
stream, attached DataOutputStream outToClient =
to socket new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';


Write out line
outToClient.writeBytes(capitalizedSentence);
to socket
}
}
} End of while loop,
loop back and wait for
another client connection

2: Application Layer 94
Chapter 2: Application layer
2.1 Principles of 2.6 P2P applications
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
2.5 DNS

2: Application Layer 95
Socket programming with UDP

UDP: no connection between client and server


no handshaking
sender explicitly attaches IP address and port of
destination to each packet
server must extract IP address, port of sender from
received packet
UDP: transmitted data may be received out of order, or lost

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

create socket, create socket,


port= x. clientSocket =
serverSocket = DatagramSocket()
DatagramSocket()
Create datagram with server IP and
port=x; send datagram via
read datagram from clientSocket
serverSocket

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

2: Application Layer 97
Example: Java client (UDP)
keyboard monitor

inFromUser
input
stream

Client
Process
Input: receives
process
packet (recall
Output: sends thatTCP received
packet (recall byte stream)

receivePacket
sendPacket
that TCP sent UDP
packet
UDP
packet
byte stream)
client
clientSocket UDP
socket UDP
socket

to network from network

2: Application Layer 98
Example: Java client (UDP)
import java.io.*;
import java.net.*;

class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Translate
InetAddress IPAddress = InetAddress.getByName("hostname");
hostname to IP
address using DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

String sentence = inFromUser.readLine();


sendData = sentence.getBytes();
2: Application Layer 99
Example: Java client (UDP), cont.
Create datagram
with data-to-send, DatagramPacket sendPacket =
length, IP addr, port new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

Send datagram clientSocket.send(sendPacket);


to server
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
Read datagram
clientSocket.receive(receivePacket);
from server
String modifiedSentence =
new String(receivePacket.getData());

System.out.println("FROM SERVER:" + modifiedSentence);


clientSocket.close();
}
}

2: Application Layer 100


Example: Java server (UDP)
import java.io.*;
import java.net.*;

class UDPServer {
public static void main(String args[]) throws Exception
Create {
datagram socket
DatagramSocket serverSocket = new DatagramSocket(9876);
at port 9876
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

while(true)
{
Create space for
DatagramPacket receivePacket =
received datagram
new DatagramPacket(receiveData, receiveData.length);
Receive serverSocket.receive(receivePacket);
datagram
2: Application Layer 101
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
InetAddress IPAddress = receivePacket.getAddress();
port #, of
sender int port = receivePacket.getPort();

String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes();
Create datagram
DatagramPacket sendPacket =
to send to client new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram serverSocket.send(sendPacket);
to socket }
}
} End of while loop,
loop back and wait for
another datagram
2: Application Layer 102
Summary
application architectures
client-server
P2P
hybrid
application service requirements:
reliability, bandwidth, delay
Internet transport service model
connection-oriented, reliable: TCP
unreliable, datagrams: UDP
specific protocols:
HTTP
FTP
SMTP, POP, IMAP
DNS
P2P: BitTorrent, Skype
socket programming
2: Application Layer 103
Assignment
1. Use the above sample code to implement the basic Java
Server/Client examples (both TCP and UDP). (You can use
different port numbers on a single PC for initial testing)
2. Form a group of three/four among your selves and implement the
server and the client on a separate PC (one as a server and the
other as a client) using the same code and logic in 1 above.
3. Once you have your client/server code up working:
Design a chat application used for instant messaging (your design must include
the user interface, the basic change you need to make on your initial
client/server application code to change it to chat application)
Implement your design.
Produce a report that includes your design, a well commented code, sample
screen shots that demonstrate your system.
Demonstrate your work in the class.

Submit your report two weeks from today. Equal participation of every member in any
part of the work is assumed (which means that, if the case is not clearly indicated in
the report, the instructor may ask any of the members for explanation of any part of
the work and failure by one member will apply to all members).
2: Application Layer 104

You might also like