You are on page 1of 147

Chapter 2

Application Layer

2: Application Layer

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 directory

2.6 P2P file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

service

2: Application Layer

Chapter 2: Application Layer


Network applications:
If we could not conceive of any useful applications,
there would not be any need to design networking
protocols to support them
Applications

Classic text based applications


Multimedia applications

2: Application Layer

Chapter 2: Application Layer


Our goals:
Conceptual and
implementation
aspects of network
applications

Define key application


layer concepts
transport-layer
service models
client-server paradigm
peer-to-peer paradigm

learn about protocols

by examining popular
application-level
protocols

HTTP
FTP
SMTP / POP3 / IMAP
DNS

programming network

applications

socket API

2: Application Layer

Some network apps


Web

Internet telephone

File transfer

Real-time video

E-mail
Instant messaging
Remote login
P2P file sharing
Multi-user network

conference
Massive parallel
computing
DNS directory
service

games
Streaming stored
video clips

2: Application Layer

Creating a network app


Write programs that

run on different end


systems and
communicate over a
network.
e.g., Web: Web server
software communicates
with browser software

No software written for


devices in network core

Network core devices do


not function at app layer
This design allows for
rapid app development

application
transport
network
data link
physical

application
transport
network
data link
physical

application
transport
network
data link
physical

2: Application Layer

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 - directory

2.6 P2P file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

service

2: Application Layer

Application architectures
Broad architectural plan of the application
Application Architecture vs Network Architecture
Network architecture is fixed and provide services to
applications
Application architecture is designed by the application
developer and dictates how the application is organized
over the various end systems
Three architectures
Client-server
Peer-to-peer (P2P)
Hybrid of client-server and P2P

2: Application Layer

Client-server architecture
server:

always-on host
Provide services
permanent IP address
server farms for scaling

Single host incapable of serving


multiple clients
Cluster of hosts are used to create
a powerful virtual server

clients:

Example:

Web, file transfer, remote


login and email

Sometimes on or always on
communicate with server
may be intermittently connected
may have dynamic IP addresses
do not communicate directly
with each other
2: Application Layer

Pure P2P architecture


no always-on server
peers - arbitrary end systems

directly communicate
peers are intermittently
connected and change IP
addresses
example: Gnutella

open source P2P file-sharing


application
any host can request files, send
files, query to find where a file
is located, respond to queries
and forward queries
2: Application Layer

10

Pure P2P architecture (2)


Scalability
Highly scalable
Example: P2P file sharing application

Millions of peers participate


Functioning as a server
Contributing resources to the community
Requesting files as well as Adding service capacity to the
system by responding to requests
Scalable each additional peer not only increase demand but
also increase service capacity

2: Application Layer

11

Pure P2P architecture (3)


Difficult to manage
Because of highly distributed and decentralized nature
Example: P2P file sharing application

One peer may have the only copy of an important file and that
peer can drop out of the community at any time

2: Application Layer

12

Hybrid of client-server and P2P


Napster

MP3 file sharing application


File transfer P2P
File search centralized:

Peers register content at central server


Peers query same central server to locate content

Instant messaging

Chatting between two users is P2P


Presence detection/location centralized:

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

13

Processes communicating
Process: program running within a host.
within same host, two processes communicate using
inter-process communication (defined by OS).
processes in different hosts communicate by
exchanging messages
A sending process creates and sends messages into
the network; a receiving process receives these
messages and possibly responds by sending messages
back.
Processes communicate with each other by using the
application layer of the five layer protocol stack
2: Application Layer

14

Client and Server Processes


A pair of processes
Client process: process that initiates communication
Server process: process that waits to be contacted
Eg. Web client/server
Client side and server side of an application

Note: applications with P2P architectures have

client processes & server processes

2: Application Layer

15

Sockets
process sends/receives

messages to/from its socket


Analogy

A process is analogous to house


socket analogous to door
sending process shoves
message out door
sending process relies on
transport infrastructure on
other side of door which brings
message to socket at receiving
process

host or
server

host or
server

process

controlled by
app developer

process
socket

socket
TCP with
buffers,
variables

Internet

TCP with
buffers,
variables

controlled
by OS

Socket is the interface between the application layer and the transport

layer within a host


It is the programming interface with which network applications are built
2: Application Layer

16

Sockets (2)
API between the application

and the network


Application developer has

Control of everything on
application layer side of socket
Little control of the transport
layer side of the socket

The control on transport

layer side

host or
server

host or
server

process

controlled by
app developer

process
socket

socket
TCP with
buffers,
variables

Internet

TCP with
buffers,
variables

controlled
by OS

choice of transport protocol;


2) ability to fix a few parameters max buffer and max segment
sizes
1)

2: Application Layer

17

Addressing processes
For a process to receive

messages, it must have


an identifier
A host has a unique32bit IP address
Q: does the IP address
of the host on which the
process runs suffice for
identifying the process?
Answer: No, many
processes can be running
on same host

Identifier includes

both the IP address


and port numbers
associated with the
process on the host.
Example port numbers:

HTTP server: 80
Mail server: 25

More on this later

2: Application Layer

18

App-layer protocols

An application layer protocol defines how an applications

processes, running on different end systems, pass


messages to each other.
An application protocols defines:
o Types of messages

exchanged, eg, request &


response messages
o Syntax of message types:
what fields in messages &
how fields are delineated
o Semantics of the fields, ie,
meaning of information in
fields
o Rules for when and how
processes send & respond
to messages

Public-domain protocols:
defined in RFCs
allows for
interoperability
eg, HTTP, SMTP
Proprietary protocols:
eg, KaZaA

2: Application Layer

19

Distinction between network applications and


application layer protocols
An application layer protocol is only one piece of a

network application.
Example web client/server application

Web application consists of many components standard for


document formats (HTML), web browsers, web servers, and
an application layer protocol (HTTP).

Example - Internet e-mail application


Mail servers that house user mailboxes, mail readers, a
standard for defining the structure of email message and a
application layer protocol (SMTP) that defines
how messages are passed between servers,
how messages are passed between servers and mail readers,
how the contents of certain parts of the mail message are to be
interpreted.
2: Application Layer

20

What transport service does an app need?


Socket interface between app process and transport protocol
More than one transport protocol - train vs airplane

Applications service requirements along three dimensions: data

loss, bandwidth, and timing

Data loss-reliable data transfer


some apps (e.g., audio) can
tolerate some loss
other apps (e.g., file transfer,
telnet) require 100% reliable
data transfer

Timing

some apps (interactive

realtime applications)
require low delay to be
effective
Eg. Internet telephony,
teleconferencing,
interactive games

Bandwidth
some apps (e.g., multimedia)

require minimum amount of


bandwidth to be effective
Internet Telephony
encodes voice at 32 kbps
o Adaptive coding techniques
o

other apps (elastic apps)

make use of whatever


bandwidth they get
o

Email, file transfer, web


transfer

2: Application Layer

21

Transport service requirements of common apps


Data loss

Bandwidth

Time Sensitive

file transfer
e-mail
Web documents
real-time audio/video

no loss
no loss
no loss
loss-tolerant

no
no
no
yes, 100s msec

stored audio/video
interactive games
instant messaging

loss-tolerant
loss-tolerant
no loss

elastic
elastic
elastic
audio: 5kbps-1Mbps
video:10kbps-5Mbps
same as above
few kbps up
elastic

Application

yes, few secs


yes, 100s msec
yes and no

2: Application Layer

22

Internet transport protocols services


TCP service:

connection-oriented:

setup required between client and server processes,

handshaking, full-duplex connection, connection-oriented vs connection service,

Data transfer
connection termination

reliable transport between sending and receiving process

Without error and in proper order, no missing or duplicate bytes

flow control: sender wont overwhelm receiver


congestion control: throttle sender when network overloaded
does not provide: timing (delay guarantees), minimum bandwidth
guarantees

TCP guarantees delivery of data, but provides not


guarantees on the rate of delivery or on the delays
experienced
2: Application Layer

23

Internet transport protocols services (2)


UDP service:
lightweight, connectionless, no handshaking
unreliable data transfer between sending and receiving

process no guarantee, out of order


does not provide: connection setup, reliability, flow control,
congestion control, timing, or bandwidth guarantee

2: Application Layer

24

Internet apps: application, transport protocols


Application
e-mail
remote terminal access
Web
file transfer
streaming multimedia
Internet telephony

Application
layer protocol

Underlying
transport protocol

SMTP [RFC 2821]


Telnet [RFC 854]
HTTP [RFC 2616]
FTP [RFC 959]
proprietary
(e.g. RealNetworks)
proprietary
(e.g., Dialpad)

TCP
TCP
TCP
TCP
TCP or UDP
typically UDP

2: Application Layer

25

Chapter 2: Application layer


2.1 Principles of

network applications

app architectures
app requirements

2.2 Web and HTTP


2.3 FTP
2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2.5 DNS-directory

service

2: Application Layer

26

Web and HTTP


Introduction
Until 1990s, the internet was used primarily by
researchers, academics and university students

Remote login, file transfer, send/receive emails

In the early 1990s, WWW, general public


Electronic communication technologies
Telephone 1870s
Broadcast radio/television 1920s and 1930s
Internet email and web
Web operates on demand and more features
Users receive what they want, when they want it.
2: Application Layer

27

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:
http://www.someschool.edu : port/someDept/pic.gif
host name

path name

Method or protocol:// host:port/path


2: Application Layer

28

Web and HTTP


First some jargon (2)
Browser

A browser is a user agent for the web;


It displays the requested web page to the user and
Provides numerous navigational and configuration
features.
Implement client side of HTTP.
Examples : netscape communicator, microsoft internet
explorer

2: Application Layer

29

Web and HTTP


First some jargon (3)
Web server

A web server houses web objects, each addressable by a


URL.
Implement server side of HTTP
Examples : Apache and Microsoft Internet Information
Server.

2: Application Layer

30

HTTP overview
HTTP: hypertext transfer
protocol
Webs application layer protocol
client/server model

client: browser that requests,


receives, displays Web
objects
server: Web server sends
objects in response to requests
HTTP 1.0: RFC 1945 before 1997
HTTP 1.1: RFC 2616 beginning1998

HT
TP
r

equ
est
PC running HT
TP
res
Explorer
pon
se

st
e
u
eq
r
se Server
P
n
T
o
p
running
HT
es
r
TP
Apache Web
T
H
server

Mac running
Navigator

HTTP defines the structure of the messages and how the

client and server exchange the messages.

2: Application Layer

31

HTTP overview (continued)


Uses TCP:
client initiates TCP

connection (creates socket)


to server, port 80
server accepts TCP
connection from client
HTTP messages (applicationlayer protocol messages)
exchanged between browser
(HTTP client) and Web
server (HTTP server)
through socket interface.
TCP connection closed

HTTP is stateless
server maintains no

information about past


client requests
Request for same object

aside

Protocols that maintain


state are complex!
past history (state) must
be maintained
if server/client crashes,
their views of state may
be inconsistent, must be
reconciled
2: Application Layer

32

HTTP connections
Nonpersistent HTTP
At most one object is
sent over a TCP
connection.
HTTP/1.0 uses
nonpersistent HTTP

Persistent HTTP
Multiple objects can
be sent over single
TCP connection
between client and
server.
HTTP/1.1 uses
persistent connections
in default mode

2: Application Layer

33

Nonpersistent HTTP
Steps of transferring a web page from server to client
Suppose user enters URL

(contains text,
references to 10
www.someSchool.edu/someDepartment/home.index
jpeg images)

1a. HTTP client initiates TCP

connection to HTTP server


(process) at
www.someSchool.edu on port 80

2. HTTP client sends HTTP

time

request message (containing


URL) into TCP connection
socket. Message indicates
that client wants object
someDepartment/home.index

1b. HTTP server at host

www.someSchool.edu waiting
for TCP connection at port 80.
accepts connection,
notifying client

3. HTTP server receives request


message, forms response
message containing requested
object, and sends message
into its socket
2: Application Layer

34

Nonpersistent HTTP (cont.)


4. HTTP server closes TCP
5. HTTP client receives response

time

message. The TCP connection


terminates. The message
indicates that the encapsulated
object is an HTML file. The
client extracts the file from the
response message, examines the
HTML file, and finds references
to 10 JPEG objects. displays
html.

6. Steps 1-5 repeated for each of

connection.

Two different browsers may

10 jpeg objects

interpret a web page in


somewhat different ways.
In this example, 11 TCP
connections are generated.
10 serial TCP connections or
some parallel connections.
Users can configure modern
browsers to control the
degree of parallelism.
Default, open 5 to 10 parallel
TCP connections
2: Application Layer

35

Response time modeling

Estimate the amount of time that elapses from when a client requests
the base HTML file until the entire file is received by the client

Definition of RTT: time to

send a small packet to travel


from client to server and
back.
RTT includes dproc , dprop , dqueue

Response time:
one RTT to initiate TCP

connection
one RTT for HTTP request
and first few bytes of HTTP
response to return
file transmission time

total = 2RTT+transmit time

initiate TCP
connection
RTT
request
file
RTT
file
received
time

time to
transmit
file

time
2: Application Layer

36

Persistent HTTP
Nonpersistent HTTP issues:

Brand new connection for each object


requires 2 RTTs per object
OS must work and allocate host
resources for each TCP connection TCP buffer and TCP variables
but browsers often open parallel TCP
connections to fetch referenced
objects

Persistent HTTP

server leaves connection open after


sending response
subsequent HTTP messages between
same client/server are sent over the
same connection
Single connection

Entire web page


Multiple web pages from the same server to
the same client

HTTP server closes the connection


when it is not used for a certain time
(a configurable timeout interval)

Persistent without pipelining:

client issues new request only


when previous response has been
received
one RTT for each referenced
object
Connection idles wastes server
resources

Persistent with pipelining:

default in HTTP/1.1
client sends requests as soon as it
encounters a referenced object
Back-to-back requests new
request before receiving a
response to a previous request
as little as one RTT for all the
referenced objects

2: Application Layer

37

HTTP request message


two types of HTTP messages:

request, response

HTTP request message:


ASCII (human-readable format)
request line
(GET, POST,
HEAD commands)

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
of message

(extra carriage return, line feed)

2: Application Layer

38

HTTP request message: general format

2: Application Layer

39

Uploading form input


Post method:
Post method is used when the

client provides some information


for the server
For example, it can be used to
send input to the server
Web page often includes form
input
Input is uploaded to server in
entity body

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

www.somesite.com/animalsearch?monkeys&banana
2: Application Layer

40

Method types
HTTP/1.0
GET
POST
HEAD

When the client wants some


information about the document
but not the document itself
asks server to leave requested
object out of response
It is similar to GET, but the
response from the server does
not contain the body.
Application developers often use
the HEAD method for debugging

HTTP/1.1
GET, POST, HEAD
PUT

Used in conjunction with web


publishing tools
uploads file in entity body to path
specified in URL field
Used by the client to provide a new or
replacement document to be stored
on the server.
The document is included in the body
of the request and will be stored in
the location defined by the URL

DELETE

Used to remove a document on the


server
deletes file specified in the URL field
2: Application Layer

41

HTTP response message


status line
(protocol
status code
status phrase)
header
lines

Entity body
(data), e.g.,
requested
HTML file

HTTP/1.1 200 OK
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 data data data data ...

2: Application Layer

42

HTTP response message: general format

2: Application Layer

43

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: header of the response message)

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

44

Trying out HTTP (client side) for yourself


1. Telnet to your favorite Web server:
telnet cis.poly.edu 80

Opens TCP connection to port 80


(default HTTP server port) at cis.poly.edu.
Anything typed in sent
to port 80 at cis.poly.edu

2. Type in a GET HTTP request:


GET /~ross/ HTTP/1.1
Host: cis.poly.edu

By typing this in (hit carriage


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

45

Header
Exchanges additional information between the client and the server
For example, the client can request that the document be sent in a special format or
the server can send extra information about the document.
The header can be one or more header lines

Categories of header lines


General header, request header, response header, entity header

Header name

Header value

2: Application Layer

46

Header (2)
Request line

Status line

General headers

General headers

Request headers

Response headers

Entity headers

Entity headers

A blank line

A blank line

Body

Body

Request message

Response message
2: Application Layer

47

General Header
The general header gives general information about

the message and can be present in both a request


and a response

Header

Description

Cache-control

Specifies information about caching

Connection
Date

Shows whether the connection should be closed


or not
Shows the current date

MIME-version

Shows the MIME version used

Upgrade

Specifies the preferred communication protocol

2: Application Layer

48

Request Header
Can be present only in a request message
It specifies the clients configuration and the clients

preferred document format

Header

Description

Accept

Shows the media format the client can accept

Accept-charset

Shows the character set the client can handle

Accept-encoding

Shows the encoding scheme the client can handle

Accept-language

Shows the language the client can accept

Authorization

Shows what permissions the client has

From

Shows the email address of the user

Host

Shows the host and port number of the client

If-modified-since

Send the document if newer than specified date

If-match

Send the document only if it matches given tag

If-non-match

Send the document only if it does not match given tag


2: Application Layer

49

Request Header (2)


Header

Description

If-range

Send only the portion of the document that is missing

If-unmodified-since

Send the document if not changed since specified date

Referrer

Specifies the URL of the linked document

User-agent

Identifies the client program

2: Application Layer

50

Response Header
Can be present only in a response message
It specifies the servers configuration and special information

about the request

Header

Description

Accept-range
Age

Shows if server accepts the range requested by the


client
Shows the age of the document

Public

Shows the specified list of methods

Retry-after

Specifies the date after which the server is available

Server

Shows server name and version number

2: Application Layer

51

Entity Header
Gives information about the body of the document
Although it is present in response messages, some request messages, such

as POST or PUT methods, that contain a body also use this type of header

Header

Description

Allow

List valid methods that can be used with a URL

Content-encoding

Specifies the encoding scheme

Content-language

Specifies the language

Content-length

Shows the length of the document

Content-range

Specifies the range of the document

Content-type

Specifies the media type

Etag

Gives an entity tag

expires

Gives the date and time when contents may change

Last-modified

Gives the date and time of the last change

location

Specifies the location of the created or moved


document
2: Application Layer

52

How many Header lines?


How does a browser decide which header lines to

include in a request message?

A browser will generate header lines as a function of


The browser type and version
The user configuration of the browser
Whether the browser currently has a cached , but possibly out-ofdate, version of the object

How does a web server decide which header lines to

include in a response message?

There are different products, versions and configurations

2: Application Layer

53

Methods
PATCH method
PATCH is similar to PUT except that the request contain
only a list of differences that should be implemented in
the existing file.
COPY method
The COPY method is used to copy a file to another location
The location of the source file is given in the request line
(URL)
The location of the destination is given in the entity
header.

2: Application Layer

54

Methods (2)
MOVE method
The MOVE method is used to move a file to another
location.
The location of the source file is given in the request line
(URL)
The location of the destination is given in the entity header
LINK method
The LINK method is used to create a link or links from a
document to another location
The location of the file is given in the request line (URL)
The location of the destination is given in the entity header

2: Application Layer

55

Methods (3)
UNLINK method
The UNLINK method is used to delete links created by the
LINK method
OPTION method
The OPTION method is used by the client to ask the
server about available options.

2: Application Layer

56

Status Codes
Code

Phrase

Description

Informational
100

Continue

101

Switching

The initial part of the request has been


received and the client may continue with its
request
The server is complying with a client request to
switch protocols defined in the upgrade header

Success
200

OK

The request is successful

201

Created

A new URL is created

202

Accepted

204

No Content

The request is accepted, but it is not


immediately acted upon
There is no content in the body

2: Application Layer

57

Status Codes (2)


Code

Phrase

Description

Redirection
301

Multiple choices The requested URL refers to more than one


resource
302
Moved
The requested URL is no longer used by the
permanently
server
304
Moved
The requested URL has moved temporarily
temporarily
Client error
400

Bad request

There is a syntax error in the request

401

Unauthorized

The request lacks proper authorization

403

Forbidden

Service is denied

404

Not found

The document is not found

405

Method not
allowed
Not acceptable

The method is not supported in this URL

406

The format requested is not acceptable


2: Application Layer

58

Status Codes (3)


Code

Phrase

Description

Server error
500
501
503

Internal server
error
Not
implemented
Service
unavailable

There is an error, such as a crash, in the


server site
The action requested can not be performed
The service is temporarily unavailable, but may
be requested in the future

2: Application Layer

59

User-server Interaction: cookies


HTTP server is Stateless
Simplifies server design
Allow to develop high performance web server
Desirable for a web site to identify users. Why?
Server to restrict user access
It wants to serve content as a function of the user identity
HTTP uses cookies.
Cookies defined in RFC 2109
Allow sites to keep track of users
Many major websites use cookies
portals, e-commerce and advertising sites

2: Application Layer

60

User-server interaction: cookies (2)


Four components:
1) a cookie header line in
the HTTP response
message
2) a cookie header line in
HTTP request message
3) a cookie file kept on
users host and managed
by users browser
4) a back-end database at
Web site

Example:

Susan access Internet


always from same PC
She visits a specific ecommerce site for first
time
the site uses cookies
When initial HTTP
requests arrives at site,
site creates a unique ID
and creates an entry in
backend database for
ID
2: Application Layer

61

Cookies: keeping state (cont.)


client

ebay: 8734
Cookie file
amazon: 1678
ebay: 8734

usual http request msg


usual http response +

Set-cookie: 1678
usual http request msg

cookie: 1678
usual http response msg

one week later:


Cookie file
amazon: 1678
ebay: 8734

usual http request msg

cookie: 1678
usual http response msg

n
server
da try i
t ab n b
creates ID
as ac
e
ke
nd
1678 for user

cookiespecific
action

ss
acce

ac
ce
ss

Cookie file

server

cookiespectific
action
2: Application Layer

62

Cookies (continued)
What cookies can bring:
authorization
shopping carts
recommendations
user session state
(Web e-mail)

aside

Cookies and privacy:


cookies permit sites to
learn a lot about you
you may supply name
and e-mail to sites
search engines use
redirection & cookies
to learn yet more
advertising companies
obtain info across
sites
2: Application Layer

63

HTTP Content
HTTP is used to transfer web pages (HTML files), GIFs,

JPEGs, Java Applets and so on.


HTTP is often used to transfer many other sorts of files.
For example

In electronic commerce applications, to transfer XML files

HTTP is also used to transfer VoiceXML, WML and other

XML document types


HTTP is often used as the file transfer protocol for P2P file
sharing
HTTP is frequently used for streaming stored audio and video
content.

2: Application Layer

64

Web caches (proxy server)


Goal: satisfy client request without involving origin server
user sets browser: Web

accesses via cache


browser sends all HTTP
requests to cache

Browser establishes a TCP


connection to the web
cache
object in cache: cache
returns object
else cache requests
object from origin server,
then returns object to
client

origin
server

Proxy
HT
TP
est
u
q
req server
re
H
u
P
e
T
se
T
st
client TP
n
T
o
H
p
res
res
pon
P
se
TT
H
st
e
u
req
P
nse
T
o
p
HT
es
r
TP
T
H
client

origin
server
2: Application Layer

65

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 (but so
does P2P file sharing)

2: Application Layer

66

Caching example
Assumptions
average object size = 100,000
bits
avg. request rate from
institutions browsers to
origin servers = 15/sec
delay from institutional router
to any origin server and back
to router = 2 sec
Consequences

origin
servers
public
Internet

1.5 Mbps
access link
institutional
network

10 Mbps LAN

utilization on LAN = 15% [(15 requests/sec)(100kbits/request)/(10Mbps)=0.15]


utilization on access link = 100% [(15 requests/sec)(100kbits/request)/(1.5Mbps)=1]
total delay = Internet delay + access delay + LAN delay
= 2 sec + minutes + milliseconds

2: Application Layer

67

Caching example (cont)


Possible solution
increase bandwidth of access
link to, say, 10 Mbps
Consequences

origin
servers
public
Internet

utilization on LAN = 15%


utilization on access link = 15%

= Internet delay +
access delay + LAN delay
= 2 sec + msecs + msecs
often a costly upgrade

10 Mbps
access link

Total delay

institutional
network

10 Mbps LAN

2: Application Layer

68

Caching example (cont)


Install cache

suppose hit rate is .4

Consequence

origin
servers
public
Internet

40% requests will be

satisfied almost immediately


60% requests satisfied by
origin server
utilization of access link
reduced to 60%, resulting in
negligible delays (say 10
msec)
total avg delay = Internet
delay + access delay + LAN
delay = .6*(2.01) secs +
0.4*(0.01 seconds) slightly >
1.2 secs

1.5 Mbps
access link
institutional
network

10 Mbps LAN

institutional
cache
2: Application Layer

69

Conditional GET
Caching reduces response

time
Problem

The copy of the object residing


in the cache may be stale

HTTP has a mechanism that

allows a cache to verify that


its objects are up to date.
Conditional GET

GET method
If-modified-since: <date>
header line

2: Application Layer

70

Conditional GET (2)


Example
o On behalf of a requesting

browser, a proxy cache


sends a request message
to a web server

GET /fruit/kiwi.gif
HTTP/1.1
Host:
www.exotiquecuisine.com

o The web server sends

the response message


with the requested
object to the cache

HTTP/1.1 200 OK
Date: Mon, 7 jul 2003, 15:39:29
Server: Apache/1.3.0 (Unix)
Last-Modified: Wed, 2 jul 2003 09:23:24
Content-Type: image/gif
(data

data

data

data ...)

Example (contd)
o The cache forwards the

object to browser and


cache the object locally
o Cache also stores the
last-modified date
along with the object
o One week later

o Another browser request

the same object via the


cache.

Cache performs a check by


issuing conditional GET

2: Application Layer

71

Conditional GET (3)


cache: specify date of

cached copy in HTTP request


If-modified-since:
<date>

server: response contains no

object if cached copy is upto-date:

HTTP/1.1 304 Not Modified


Date: Mon, 14 Jul 2003 15:39:29
Server: Apache/1.3.0 (Unix)
(empty entity body)

server

cache
HTTP request msg
If-modified-since:
<date>

HTTP response

object
not
modified

HTTP/1.1
304 Not Modified

HTTP request msg


If-modified-since:
<date>

HTTP response

object
modified

HTTP/1.1 200 OK

<data>
2: Application Layer

72

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 file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

73

FTP File Transfer Protocol


Some problems
Different file name conventions
Different ways to represent text and data
Different directory structures

2: Application Layer

74

FTP: the file transfer protocol

user
at host

FTP
FTP
user
client
interface

file transfer

local file
system

FTP
server
remote file
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

75

FTP: separate control, data connections


TCP control connection
port 21

FTP client contacts FTP server

at port 21, specifying TCP as


transport protocol
Client sends authorization over
control connection
Client browses remote
directory by sending commands
over control connection.
When server receives a
command for a file transfer,
the server opens a TCP data
connection to client
After transferring one file,
server closes connection.

FTP
client

TCP data connection


port 20

FTP
server

Server opens a second TCP

data connection to transfer


another file.
Control connection: out of
band
FTP server maintains state:
current directory, earlier
authentication
2: Application Layer

76

FTP commands, responses


Sample commands:
sent as ASCII text over

control channel
USER username
PASS password

LIST return list of file in

Sample return codes


status code and phrase (as

current directory

RETR filename retrieves

(gets) file

STOR filename stores

(puts) file onto remote


host

in HTTP)
331 Username OK,
password required
125 data connection
already open;
transfer starting
425 Cant open data
connection
452 Error writing
file

For other FTP commands and replies refer RFC

959
2: Application Layer

77

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 file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

78

Electronic Mail

outgoing
message queue
user mailbox
user
agent

Three major components:


user agents
mail servers

mail
server

simple mail transfer protocol:

SMTP

SMTP

User Agent
a.k.a. mail reader
composing, editing, reading mail
messages
e.g., Eudora, Outlook, elm,
Netscape Messenger all GUI
based
outgoing, incoming messages
stored on server

SMTP

mail
server

user
agent

SMTP

user
agent
mail
server

user
agent

user
agent

user
agent

2: Application Layer

79

Electronic Mail: mail servers


user
agent

Mail Servers
mailbox contains incoming

messages for user


message queue of outgoing
(to be sent) mail messages
reattempts after every 30
minutes or so.
SMTP protocol between mail
servers to send email
messages
client: sending mail server
server: receiving mail
server

mail
server

SMTP
SMTP
mail
server

user
agent

SMTP

user
agent
mail
server

user
agent

user
agent

user
agent

2: Application Layer

80

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

81

Scenario: Alice sends message to Bob


4) SMTP client sends Alices
message over the TCP
connection
5) Bobs mail server places the
message in Bobs mailbox
6) Bob invokes his user agent
to read message

1) Alice uses UA to compose


message and to
bob@someschool.edu
2) Alices UA sends message
to her mail server; message
placed in message queue
3) Client side of SMTP opens
TCP connection with Bobs
mail server

1
user
agent

mail
server
3

mail
server
4

user
agent

2: Application Layer

82

Sample SMTP interaction


S:
C:
S:
C:
S:
C:
S:
C:
S:
C:
C:
C:
S:
C:
S:

220 hamburger.edu
HELO crepes.fr
250 Hello crepes.fr, pleased to meet you
MAIL FROM: <alice@crepes.fr>
250 alice@crepes.fr... Sender ok
RCPT TO: <bob@hamburger.edu>
250 bob@hamburger.edu ... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Do you like ketchup?
How about pickles?
.
250 Message accepted for delivery
QUIT
221 hamburger.edu closing connection
2: Application Layer

83

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

84

SMTP: final words

Comparison with HTTP:


HTTP: pull
SMTP: push

SMTP uses persistent

connections
SMTP requires message
(header & body) to be in 7bit ASCII
SMTP server uses
CRLF.CRLF to determine
end of message

SMTP: ASCII

command/response interaction,
status codes

HTTP: does not impose this

restriction

HTTP: each object

encapsulated in its own


response msg
SMTP: multiple objects sent in
multipart msg
2: Application Layer

85

Mail message format


SMTP: protocol for
exchanging email msgs
RFC 822: standard for text
message format:
header lines, e.g.,
To:
From:
Subject:
different from SMTP
commands!

header

blank
line

body

body

the message, ASCII


characters only
2: Application Layer

86

Message format: multimedia extensions


MIME: Multipurpose Internet Mail Extensions,

multimedia mail extension, RFC 2045, 2046


additional lines in msg header declare MIME content
type
MIME version
method used
to encode data
multimedia data
type, subtype,
parameter declaration
encoded data

From: alice@crepes.fr
To: bob@hamburger.edu
Subject: Picture of yummy crepe.
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
base64 encoded data .....
.........................
......base64 encoded data

2: Application Layer

87

Received Message
Header lines inserted by the SMTP receiving server
The receiving appends a Received: header line

Received: from crepes.fr by hamburger.edu; 12 oct 98 15:27:39 GMT


From: alice@crepes.fr
To: bob@hamburger.edu
Subject: Picture of yummy crepe.
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
base64 encoded data .....
.........................
......base64 encoded data

2: Application Layer

88

Mail access protocols


user
agent

SMTP

SMTP

senders mail
server

access
protocol

user
agent

receivers mail
server

Mail access uses client server architecture


Placing a mail server on local PC

SMTP: delivery/storage to receivers server


Mail access protocol: retrieval from server

POP: Post Office Protocol [RFC 1939]


Port 110, TCP
authorization (agent <-->server) , transaction, and update

IMAP: Internet Mail Access Protocol [RFC 1730]


more features (more complex)
manipulation of stored msgs on server

HTTP: Hotmail , Yahoo! Mail, etc.


2: Application Layer

89

POP3 protocol

authorization phase

client commands:
user: declare username
pass: password
server responses
+OK
-ERR

transaction phase
Retrieves messages, mark messages for
deletion, remove deletion marks, obtain
mail statistics
list: list message numbers
retr: retrieve message by number
dele: delete
Quit

update phase
Delete the messages that were marked for
deletion

S:
C:
S:
C:
S:

+OK POP3 server ready


user bob
+OK
pass hungry
+OK user successfully logged

C:
S:
S:
S:
C:
S:
S:
C:
C:
S:
S:
C:
C:
S:

list
1 498
2 912
.
retr 1
<message 1 contents>
.
dele 1
retr 2
<message 1 contents>
.
dele 2
quit
+OK POP3 server signing off
2: Application Layer

90

on

POP3 Protocol (2)


Try yourself telnet yourself directly into a POP 3
server using port 110
telnet mailServer 110
+OK POP3 server ready
user bob
+OK
pass hungry
+OK user successfully logged on

2: Application Layer

91

POP3 (more) and IMAP


More about POP3
Previous example uses
download and delete
mode.
Bob cannot re-read email if he changes client
Download-and-keep:
copies of messages on
different clients
POP3 is stateless across
sessions

IMAP RFC 2060


Keep all messages in one
place: the server
Allows user to organize
messages in folders
IMAP keeps user state
across sessions:

names of folders and


mappings between message
IDs and folder name

IMAP permits a user agent

to obtain components of
messages.
2: Application Layer

92

Web-Based E-mail
Accessing email

through web browsers


Hotmail introduced
web-based access in
the mid 1990s
User agent is web
browser
user communicates
with its remote
mailbox via HTTP

The email message is sent

from recipient's mail


server to recipient browser
using the HTTP protocol
rather than POP3 or IMAP
The email message is sent
from senders browser to
senders mail server over
HTTP rather than over
SMTP.
Senders mail server sends
messages to and receives
messages from other mail
servers using SMTP.
2: Application Layer

93

Web-Based E-mail (2)


Users can organize their

messages in a hierarchy of
folders on the remote
server.
Many implementations of
web-based e-mail use an
IMAP server to provide the
folder functionality

Access to folders and


messages is provided with
scripts that run in an
HTTP server,
The scripts use the IMAP
protocol to communicate
with an IMAP server.
2: Application Layer

94

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 file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

95

DNS: Domain Name System


A directory service that translates host names to IP addresses

People: many identifiers:

SSN, name, passport #

Internet hosts, routers:

IP address (32 bit) used for addressing


datagrams
name, e.g.,
www.yahoo.com - used
by humans

Q: map between IP
addresses and name ?

Domain Name System: UDP,


port 53
distributed database
implemented in hierarchy of
many name servers
application-layer protocol host,
routers, name servers to
communicate to resolve names
(address/name translation)
note: core Internet
function, implemented as
application-layer protocol
complexity at networks
edge

2: Application Layer

96

DNS Example
DNS is commonly employed by other application layer protocols.

Browser (HTTP client)


requests the URL
www.someschool.edu/index.
html

3.

4.

1. The same user machine runs the

client side of the DNS


application
2. The browser extracts the
hostname, www.someschool.edu,
from the URL and passes the
hostname to the client side of
the DNS application

5.

The DNS client sends a query


containing the hostname to a
DNS server
The DNS client eventually
receives a reply, which
includes the IP address for
the hostname.
Once the browser receives
the IP address from DNS, it
can initiate a TCP connection
to the HTTP server process
located at that IP address

DNS adds additional delay


2: Application Layer

97

DNS
DNS services
Hostname to IP address
translation
Host aliasing

Canonical and alias names


relay1.west-cost.enterprise.com can
have aliases enterprise.com or
www.enterprise.com

Mail server aliasing

E-mail addresses be mnemonic


Canonical name - Relay1.westcoast.hotmail.com
Alias bob@htomail.com

Load distribution
Replicated Web servers: set of
IP addresses for one canonical
name

RFC 1034 and RFC 1035


UDP port number 53
Why not centralize DNS?
single point of failure
traffic volume
distant centralized
database
Maintenance huge
database
doesnt scale!
2: Application Layer

98

Distributed, Hierarchical Database


Root DNS servers
Top-Level Domain DNS servers

Root DNS Servers

Authoritative DNS servers

com DNS servers


yahoo.com
amazon.com
DNS servers DNS servers

org DNS servers


pbs.org
DNS servers

edu DNS servers


poly.edu
umass.edu
DNS serversDNS servers

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

Client queries a root server to find com DNS server, which

returns IP address for TLD server


Client queries com DNS server to get amazon.com DNS
server, which returns IP address of authoritative server
Client queries amazon.com DNS server to get IP address
for www.amazon.com

2: Application Layer

99

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 Los Angeles)
d U Maryland College Park, MD
g US DoD Vienna, VA
h ARL Aberdeen, MD
j Verisign, ( 11 locations)

k RIPE London (also Amsterdam, Frankfurt)

Feb 2004 map

i Autonomica, Stockholm (plus 3 other locations)

m WIDE Tokyo
e NASA Mt View, CA
f Internet Software C. Palo Alto, CA (and 17
other locations)

b USC-ISI Marina del Rey, CA


l ICANN Los Angeles, CA

13 root name
servers worldwide

2: Application Layer

100

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: organizations

DNS servers, providing authoritative


hostname to IP mappings for organizations
servers (e.g., Web and mail).

Can be maintained by organization or service


provider

2: Application Layer

101

Local Name Server


Does not strictly belong to hierarchy
Each ISP (residential ISP, company,

university) has one.

Also called default name server

When a host makes a DNS query, query is

sent to its local DNS server

Acts as a proxy, forwards query into hierarchy.

2: Application Layer

102

Example

root DNS server

Host at cis.poly.edu

wants IP address for


gaia.cs.umass.edu

3
4

TLD DNS server

5
local DNS server
dns.poly.edu

requesting host

authoritative DNS server


dns.cs.umass.edu

cis.poly.edu
gaia.cs.umass.edu
2: Application Layer

103

Recursive queries
recursive query:

puts burden of name

resolution on
contacted name
server
heavy load?

iterated query:
contacted server

replies with name of


server to contact
I dont know this
name, but ask this
server

root DNS server

3
7

TLD DNS serve


local DNS server
dns.poly.edu

requesting host

authoritative DNS server


dns.cs.umass.edu

cis.poly.edu
gaia.cs.umass.edu
2: Application Layer

104

DNS: caching and updating records


root DNS server

Benefits of DNS caching

Improve the delay


performance
Reduce the number of DNS
messages around the
Internet.

once (any) name server

learns mapping, it caches


mapping

TLD DNS server

5
local DNS server
dns.poly.edu

cache entries timeout


1
8
(disappear) after some time
TLD servers typically
cached in local name servers
Thus root name servers requesting host
not often visited
cis.poly.edu

authoritative DNS server


dns.cs.umass.edu

gaia.cs.umass.edu
2: Application Layer

105

DNS records
DNS: distributed db storing resource records (RR)
RR format:
Type=A

name is hostname
value is IP address
(relay1.bar.foo.com, 145.37.93.126, A)
Provide standard hostname to IP
address mapping

Type=NS

(name, value, type, ttl)


Type=CNAME

name is alias name for some cannonical


(the real) name
www.ibm.com is really
servereast.backup2.ibm.com
value is cannonical name
(foo.com, relay1.bar.foo.com, CNAME)
Provide querying hosts the canonical
name for a host name

name is domain (e.g. foo.com)


value is hostname of authoritative Type=MX
value is canonical name of a mailserver associated
name server for this domain
with name
(foo.com, dns.foo.com, NS)
(foo.com, mail.bar.foo.com, MX)
Used to route DNS queries further
Allow hostnames of mail servers to have
simple aliases
along in the query chain
2: Application Layer

106

DNS protocol, messages


DNS protocol : query and reply messages, both with same message format
0

15 16

31

msg header
identification: 16 bit # for

query, reply to query uses


same #
flags:
Query (0) or reply (1)
recursion desired
recursion available
reply is authoritative

2: Application Layer

107

DNS protocol, messages


0
31

15 16

Name, type fields


for a query
RRs in reponse to query
(type, value, TTL)
records for
authoritative servers

additional helpful info that may be used

Eg.: the answer field in a reply to an MX query contains a resource record providing the canonical
hostname of a mail server. The additional section contains a Type A record providing the IP
address for the canonical hostname of the mail server.
2: Application Layer

108

nslookup program
How would you send a DNS query message to DNS

server?
After invoking nslookup program, you can send a DNS
query to any DNS server (root, TLD or authoritative)
After receiving the reply message from the DNS
server, nslookup will display the records included in
the reply.
As an alternative to running nslookup from your own
host, you can visit one of many web sites that allow
you to remotely employ nslookup.

2: Application Layer

109

Inserting records into DNS


Example: just created startup Network Utopia
Register name networkuptopia.com at a registrar

(e.g., Network Solutions) http://www.internic.net

Need to provide registrar with names and IP addresses of


your authoritative name server (primary and secondary)
Registrar inserts two RRs into the com TLD server:

(networkutopia.com, dns1.networkutopia.com, NS)


(dns1.networkutopia.com, 212.212.212.1, A)

Put in authoritative server Type A record for

www.networkuptopia.com and Type MX record for


mail.networkutopia.com
How do people get the IP address of your Web site?
2: Application Layer

110

Chapter 2: Application layer


2.1 Principles of

network applications

app architectures
app requirements

2.2 Web and HTTP


2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS

2.6 P2P file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

111

Introduction
More traffic on Internet than web application
MP3, videos, software, documents, images
Protocol and networking issues in P2P file sharing

2: Application Layer

112

P2P file sharing


How Alice use P2P file sharing system?
For downloading MP3
Alice chooses one of the
peers, Bob.
Example
File is copied from Bobs
Alice runs P2P client
PC to Alices notebook:
application on her
HTTP
notebook computer
While Alice downloads,
Intermittently
other users uploading from
connects to Internet;
Alice.
gets new IP address
Alices peer is both a Web
for each connection
client and a transient Web
Asks for Hey Jude
server.
Application displays
All peers are client/ servers
other peers that have
= highly scalable!
copy of Hey Jude.
2: Application Layer

113

P2P file sharing


How a peer determines which peers have the objects (i.e., files) it desires?

Connected peers having objects to share


If peer X is interested in obtaining a particular

object, then peer X must have a way of determining


the IP addresses of the connected peers that have
copies of the desired object. --- Problem peers
connects and disconnects
Three architectures for locating contents

Centralized directory Napster


Query flooding Gnutella
Exploiting heterogeneity - KaZaA

2: Application Layer

114

P2P: centralized directory


original Napster design
1) when peer connects, it
informs central server:

ma
r
o
f
In
1

centralized
directory server

Q
ue
ry
fo

rc
te
on
nt

2) Alice queries for Hey


Jude
3) Alice requests file from
Bob

peers

ansfe
r

IP address
content

date
p
u
nd

File t
r

Bob

Alice

2: Application Layer

115

P2P: problems with centralized directory


Single point of failure
Performance

bottleneck
Copyright
infringement

file transfer is
decentralized, but
locating content is
highly centralized

2: Application Layer

116

Query flooding: Gnutella


fully distributed
no central server
public domain protocol
many Gnutella clients

implementing protocol

overlay network: graph


edge between peer X
and Y if theres a TCP
connection
all active peers and
edges is overlay net
Edge is not a physical
link
Given peer will
typically be connected
with < 10 overlay
neighbors
2: Application Layer

117

Gnutella: protocol

How a peer locates and retrieves content?

Query message

sent over existing TCP


connections
peers forward
Query message
ry
e
QueryHit
it
Qu
H
y
er
sent over
u
Q
reverse
Query
path
QueryHit
Scalability:
limited scope query flooding
peer count field

Qu

File transfer:
HTTP
Query
QueryHit
Qu
ery

er
y

2: Application Layer

118

Gnutella: Peer joining


Joining peer X must find some other peer in
Gnutella network: use list of candidate peers
X sequentially attempts to make TCP with peers
on list until connection setup with Y
X sends Ping message to Y; Ping message include
peer-count field; Y forwards Ping message.
All peers receiving Ping message respond with
Pong message

1.
2.
3.
4.

5.

IP address, the number of files it is sharing and the


total number of Kbytes taken by the files it is sharing

X receives many Pong messages. It can then setup


additional TCP connections multiple edges
2: Application Layer

119

Exploiting heterogeneity: KaZaA


Proprietary technology
Encrypts all control

traffic
Each peer is either a
group leader or assigned
to a group leader.

TCP connection between


peer and its group leader.
TCP connections between
some pairs of group leaders.

Group leader tracks the

content in all its children.

o rd in a ry p e e r
g r o u p - le a d e r p e e r
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

120

KaZaA: Querying
Each file has a hash and a descriptor
Client sends keyword query to its group

leader
Group leader responds with matches:

For each match: metadata, hash, IP address

If group leader forwards query to other

group leaders, they respond with matches


Client then selects files for downloading

HTTP requests using hash as identifier sent to


peers holding desired file
2: Application Layer

121

KaZaA tricks
Techniques to improve its performance
Limit on simultaneous uploads
Request queuing
Incentive priorities
Parallel downloading
uses byte-range header of HTTP to request different
portions of the file from different peers

2: Application Layer

122

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 file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

123

Socket programming
Goal: learn how to build client/server application that
communicate using sockets
Socket API
introduced in BSD4.1 UNIX,

1981
explicitly created, used,
released by apps
client/server paradigm
two types of transport service
via socket API:
unreliable datagram
reliable, byte streamoriented

socket
a host-local,
application-created,
OS-controlled interface
(a door) into which
application process can
both send and
receive messages to/from
another application
process

2: Application Layer

124

Socket-programming using TCP


Socket: a door between application process and end-endtransport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one process
to another

controlled by
application
developer
controlled by
operating
system

process

process
socket
TCP with
buffers,
variables

host or
server

internet

socket
TCP with
buffers,
variables

controlled by
application
developer
controlled by
operating
system

host or
server
2: Application Layer

125

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

application viewpoint

TCP provides reliable, in-order


transfer of bytes (pipe)
between client and server
2: Application Layer

126

Client socket, welcoming socket


and connection socket

2: Application Layer

127

Stream jargon
keyboard

monitor

input
stream

Client
Process

output
stream

inFromServer

process

outToServer

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.

inFromUser

A stream is a sequence of

input
stream

client
TCP
clientSocket
socket
to network

TCP
socket

from network

2: Application Layer

128

Socket programming with TCP

input
stream

Client
Process
process

output
stream

inFromServer

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)

outToServer

Example client-server app:

monitor

inFromUser

keyboard

input
stream

client
TCP
clientSocket
socket
to network

TCP
socket

from network

2: Application Layer

129

Client/server socket interaction: TCP


Server

Client

(running on hostid)

create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()

TCP

wait for incoming


connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket

setup

create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket

read reply from


clientSocket
close
clientSocket
2: Application Layer

130

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
Create
client socket,
connect to server
Create
output stream
attached to socket

BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
2: Application Layer

131

Example: Java client (TCP), cont.


Create
input stream
attached to socket

BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();

Send line
to server

outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();

Read line
from server

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


clientSocket.close();
}
}
2: Application Layer

132

Example: Java server (TCP)


import java.io.*;
import java.net.*;
class TCPServer {

Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket

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


{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));

2: Application Layer

133

Example: Java server (TCP), cont


Create output
stream, attached
to socket

DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());

Read in line
from socket

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

Write out line


to socket

outToClient.writeBytes(capitalizedSentence);
}
}

End of while loop,


loop back and wait for
another client connection

2: Application Layer

134

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 file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

135

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

application viewpoint

UDP provides unreliable transfer


of groups of bytes (datagrams)
between client and server

UDP: transmitted data may be


received out of order, or
lost

2: Application Layer

136

Socket programming with UDP

receivePacket

sendPacket

inFromUser

Example client-server app:


keyboard
monitor
1) A client reads a line
from standard input
(keyboard) and sends
input
stream
the line out its socket
to the server
Client
Process
2) server reads line from
process
its socket
3) server converts the line Output: sends
to uppercase
packet (TCP sent
byte stream)
UDP
UDP
4) Server sends the
packet
packet
modified line out its
socket to the client
client
UDP
clientSocket
5) client reads the
UDP
socket
socket
modified line from its
to network
from network
socket and prints the
line on its standard
output (monitor)

Input: receives

packet (TCP
received byte
stream)

2: Application Layer

137

Client/server socket interaction: UDP


Server

(running on hostid)

create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()

read request from


serverSocket
write reply to
serverSocket
specifying client
host address,
port number

Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket

read reply from


clientSocket
close
clientSocket

2: Application Layer

138

Example: Java client (UDP)


import java.io.*;
import java.net.*;

Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS

class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
2: Application Layer

139

Example: Java client (UDP), cont.


Create datagram
with data-to-send,
length, IP addr, port
Send datagram
to server

DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);

Read datagram
from server

clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
2: Application Layer

140

Example: Java server (UDP)


import java.io.*;
import java.net.*;

Create
datagram socket
at port 9876

class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{

Create space for


received datagram
Receive
datagram

DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
2: Application Layer

141

Example: Java server (UDP), cont


String sentence = new String(receivePacket.getData());

Get IP addr
port #, of
sender

InetAddress IPAddress = receivePacket.getAddress();


int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();

Create datagram
to send to client
Write out
datagram
to socket
}

DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
serverSocket.send(sendPacket);
}
}

End of while loop,


loop back and wait for
another datagram

2: Application Layer

142

Chapter 2: Application layer


2.1 Principles of

network applications

app architectures
app requirements

2.2 Web and HTTP


2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS

2.6 P2P file sharing


2.7 Socket programming

with TCP
2.8 Socket programming
with UDP
2.9 Building a Web
server

2: Application Layer

143

Building a simple Web server


handles one HTTP

request
accepts the request
parses header
obtains requested file
from servers file
system
creates HTTP response
message:

after creating server,

you can request file


using a browser (eg IE
explorer)
see text for details

header lines + file

sends response to client


2: Application Layer

144

Chapter 2: Summary
Our study of network apps now complete!
Application architectures
client-server
P2P
hybrid
application service

requirements:

reliability, bandwidth, delay

Internet transport service

specific protocols:
HTTP
FTP
SMTP, POP, IMAP
DNS
socket programming

model

connection-oriented, reliable: TCP


unreliable, datagrams: UDP

2: Application Layer

145

Chapter 2: Summary
Most importantly: learned about protocols
typical request/reply

message exchange:

client requests info or


service
server responds with data,
status code

message formats:
headers: fields giving info
about data
data: info being
communicated

control vs. data msgs

in-band, out-of-band
centralized vs. decentralized
stateless vs. stateful
reliable vs. unreliable msg
transfer
complexity at network
edge

2: Application Layer

146

End
2: Application Layer

147

You might also like