2,Application Layer 1
Part 2,Application Layer
Our goals:
r conceptual,
implementation
aspects of network
application protocols
m client-server
paradigm
m service models
r learn about protocols
by examining popular
application-level
protocols
More chapter goals
r specific protocols,
m http
m ftp
m smtp
m pop
m dns
r programming network
applications
m socket API
2,Application Layer 2
Applications and application-layer protocols
Application,communicating,
distributed processes
m running in network hosts in
“user space”
m exchange messages to
implement application
m e.g.,email,ftp,Web
Application-layer protocols
m one,piece” of an app
m define messages
exchanged by apps and
actions taken
m use communication services
provided by lower layer
protocols (TCP,UDP)
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
2,Application Layer 3
Network applications,some jargon
Process,program running
within a host.
r within same host,two
processes communicate
using interprocess
communication (defined
by OS).
r processes running in
different hosts
communicate with an
application-layer
protocol
r user agent,software
process,interfacing
with user,above” and
network,below”,
m implements
application-level
protocol
m Web,browser
m E-mail,mail reader
m streaming
audio/video,media
player
2,Application Layer 4
Client-server paradigm
Typical network app has two
pieces,client and server
application
transport
network
data link
physical
application
transport
network
data link
physical
Client:
r initiates contact with server
(“speaks first”)
r typically requests service from
server,
r Web,client implemented in
browser; e-mail,in mail reader
request
reply
Server:
r provides requested service to client
r e.g.,Web server sends requested Web
page,mail server delivers e-mail
2,Application Layer 5
Application-layer protocols (cont).
API,application
programming interface
r defines interface
between application and
transport layers
r socket,Internet API
m two processes
communicate by sending
data into socket,reading
data out of socket
Q,how does a process
“identify” the other
process with which it
wants to communicate?
m IP address of host
running other process
m,port number” - allows
receiving host to
determine to which local
process the message
should be delivered
… lots more on this later.
2,Application Layer 6
What transport service does an app need?
Data loss
r some apps (e.g.,audio) can
tolerate some loss
r other apps (e.g.,file
transfer,telnet) require
100% reliable data
transfer
Timing
r some apps (e.g.,
Internet telephony,
interactive games)
require low delay to be
“effective”
Bandwidth
r some apps (e.g.,
multimedia) require
minimum amount of
bandwidth to be
“effective”
r other apps (“elastic
apps”) make use of
whatever bandwidth
they get
2,Application Layer 7
Transport service requirements of common apps
Application
file transfer
e-mail
Web documents
real-time audio/video
stored audio/video
interactive games
financial apps
Data loss
no loss
no loss
loss-tolerant
loss-tolerant
loss-tolerant
loss-tolerant
no loss
Bandwidth
elastic
elastic
elastic
audio,5Kb-1Mb
video:10Kb-5Mb
same as above
few Kbps up
elastic
Time Sensitive
no
no
no
yes,100’s msec
yes,few secs
yes,100’s msec
yes and no
2,Application Layer 8
Internet transport protocols services
TCP service:
r connection-oriented,setup
required between client,
server
r reliable transport between
sending and receiving process
r flow control,sender won’t
overwhelm receiver
r congestion control,throttle
sender when network
overloaded
r does not providing,timing,
minimum bandwidth
guarantees
UDP service:
r unreliable data transfer
between sending and
receiving process
r does not provide,
connection setup,
reliability,flow control,
congestion control,timing,
or bandwidth guarantee
Q,why bother? Why is
there a UDP?
2,Application Layer 9
Internet apps,application,transport protocols
Application
e-mail
remote terminal access
Web
file transfer
streaming multimedia
remote file server
Internet telephony
Application
layer protocol
smtp [RFC 821]
telnet [RFC 854]
http [RFC 2068]
ftp [RFC 959]
proprietary
(e.g,RealNetworks)
NSF
proprietary
(e.g.,Vocaltec)
Underlying
transport protocol
TCP
TCP
TCP
TCP
TCP or UDP
TCP or UDP
typically UDP
2,Application Layer 10
The Web,the http protocol
http,hypertext transfer
protocol
r Web’s application layer
protocol
r client/server model
m client,browser that
requests,receives,
“displays” Web objects
m server,Web server
sends objects in
response to requests
r http1.0,RFC 1945
r http1.1,RFC 2068
PC running
Explorer
Server
running
NCSA Web
server
Mac running
Navigator
2,Application Layer 11
The http protocol,more
http,TCP transport
service:
r client initiates TCP
connection (creates socket)
to server,port 80
r server accepts TCP
connection from client
r http messages (application-
layer protocol messages)
exchanged between browser
(http client) and Web server
(http server)
r TCP connection closed
http is,stateless”
r server maintains no
information about
past client requests
Protocols that maintain
“state” are complex!
r past history (state) must
be maintained
r if server/client crashes,
their views of,state” may
be inconsistent,must be
reconciled
aside
2,Application Layer 12
http example
Suppose user enters URL
www.someSchool.edu/someDepartment/home.index
1a,http client initiates TCP
connection to http server
(process) at
www.someSchool.edu,Port 80
is default for http server.
2,http client sends http request
message (containing URL) into
TCP connection socket
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
(someDepartment/home.index),
sends message into sockettime
(contains text,
references to 10
jpeg images)
2,Application Layer 13
http example (cont.)
5,http client receives response
message containing html file,
displays html,Parsing html
file,finds 10 referenced jpeg
objects
6,Steps 1-5 repeated for each
of 10 jpeg objects
4,http server closes TCP
connection,
time
2,Application Layer 14
Non-persistent,persistent connections
Non-persistent
r http/1.0,server parses
request,responds,closes
TCP connection
r 2 RTTs to fetch object
m TCP connection
m object request/transfer
r each transfer suffers from
TCP’s initially slow sending
rate
r many browsers open multiple
parallel connections
Persistent
r default for htp/1.1
r on same TCP connection,
server,parses request,
responds,parses new
request,..
r client sends requests for
all referenced objects as
soon as it receives base
HTML.
r fewer RTTs,less slow
start.
2,Application Layer 15
http message format,request
r two types of http messages,request,response
r http request message:
m ASCII (human-readable format)
GET /somedir/page.html HTTP/1.0
User-agent,Mozilla/4.0
Accept,text/html,image/gif,image/jpeg
Accept-language:fr
(extra carriage return,line feed)
request line
(GET,POST,
HEAD commands)
header
lines
Carriage return,
line feed
indicates end
of message
2,Application Layer 16
http request message,general format
2,Application Layer 17
http message format,response
HTTP/1.0 200 OK
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,.,
status line
(protocol
status code
status phrase)
header
lines
data,e.g.,
requested
html file
2,Application Layer 18
http response status codes
200 OK
m request succeeded,requested object later in this message
301 Moved Permanently
m requested object moved,new location specified later in
this message (Location:)
400 Bad Request
m request message not understood by server
404 Not Found
m requested document not found on this server
505 HTTP Version Not Supported
In first line in server->client response message.
A few sample codes:
2,Application Layer 19
Trying out http (client side) for yourself
1,Telnet to your favorite Web server:
Opens TCP connection to port 80
(default http server port) at www.eurecom.fr.
Anything typed in sent
to port 80 at www.eurecom.fr
telnet www.eurecom.fr 80
2,Type in a GET http request:
GET /~ross/index.html HTTP/1.0 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 20
User-server interaction,authentication
Authentication, control access
to server content
r authorization credentials,
typically name,password
r stateless,client must present
authorization in each request
m authorization,header line in
each request
m if no authorization,header,
server refuses access,
sends
WWW authenticate:
header line in response
client server
usual http request msg
401,authorization req.
WWW authenticate:
usual http request msg
+ Authorization,<cred>
usual http response msg
usual http request msg
+ Authorization,<cred>
usual http response msg time
2,Application Layer 21
Cookies,keeping,state”
r server-generated #,
server-remembered #,
later used for:
m authentication
m remembering user
preferences,previous
choices
r server sends,cookie” to
client in response msg
Set-cookie,1678453
r client presents cookie in
later requests
cookie,1678453
client server
usual http request msg
usual http response +
Set-cookie,#
usual http request msg
cookie,#
usual http response msg
usual http request msg
cookie,#
usual http response msg
cookie-
spectific
action
cookie-
spectific
action
2,Application Layer 22
Conditional GET,client-side caching
r Goal,don’t send object if
client has up-to-date
cached version
r client,specify date of
cached copy in http request
If-modified-since,
<date>
r server,response contains
no object if cached copy is
up-to-date,
HTTP/1.0 304 Not
Modified
client server
http request msg
If-modified-since,
<date>
http response
HTTP/1.0
304 Not Modified
object
not
modified
http request msg
If-modified-since,
<date>
http response
HTTP/1.1 200 OK
<data>
object
modified
2,Application Layer 23
Web Caches (proxy server)
r user sets browser,Web
accesses via web cache
r client sends all http
requests to web cache
m object in web cache,
web cache returns
object
m else web cache requests
object from origin
server,then returns
object to client
Goal,satisfy client request without involving origin server
client
Proxy
server
client
origin
server
origin
server
2,Application Layer 24
Why Web Caching?
Assume,cache is,close”
to client (e.g.,in same
network)
r smaller response time,
cache,closer” to
client
r decrease traffic to
distant servers
m link out of
institutional/local ISP
network often
bottleneck
origin
servers
public
Internet
institutional
network 10 Mbps LAN
1.5 Mbps
access link
institutional
cache
2,Application Layer 25
ftp,the file transfer protocol
r transfer file to/from remote host
r client/server model
m client,side that initiates transfer (either to/from
remote)
m server,remote host
r ftp,RFC 959
r ftp server,port 21
file transfer FTP
server
FTP
user
interface
FTP
client
local file
system
remote file
system
user
at host
2,Application Layer 26
ftp,separate control,data connections
r ftp client contacts ftp server
at port 21,specifying TCP as
transport protocol
r two parallel TCP connections
opened:
m control,exchange
commands,responses
between client,server.
“out of band control”
m data,file data to/from
server
r ftp server maintains,state”,
current directory,earlier
authentication
FTP
client
FTP
server
TCP control connection
port 21
TCP data connection
port 20
2,Application Layer 27
ftp commands,responses
Sample commands:
r sent as ASCII text over
control channel
r USER username
r PASS password
r LIST return list of file in
current directory
r RETR filename retrieves
(gets) file
r STOR filename stores
(puts) file onto remote
host
Sample return codes
r status code and phrase (as
in http)
r 331 Username OK,
password required
r 125 data connection
already open;
transfer starting
r 425 Can’t open data
connection
r 452 Error writing
file
2,Application Layer 28
Electronic Mail
Three major components:
r user agents
r mail servers
r simple mail transfer
protocol,smtp
User Agent
r a.k.a.,mail reader”
r composing,editing,reading
mail messages
r e.g.,Eudora,Outlook,elm,
Netscape Messenger
r outgoing,incoming messages
stored on server
user mailbox
outgoing
message queue
mail
server
user
agent
user
agent
user
agentmailserver
user
agent
user
agent
mail
server
user
agent
SMTP
SMTP
SMTP
2,Application Layer 29
Electronic Mail,mail servers
Mail Servers
r mailbox contains incoming
messages (yet to be read)
for user
r message queue of outgoing
(to be sent) mail messages
r smtp protocol between mail
servers to send email
messages
m client,sending mail
server
m,server”,receiving mail
server
mail
server
user
agent
user
agent
user
agentmailserver
user
agent
user
agent
mail
server
user
agent
SMTP
SMTP
SMTP
2,Application Layer 30
Electronic Mail,smtp [RFC 821]
r uses tcp to reliably transfer email msg from client to
server,port 25
r direct transfer,sending server to receiving server
r three phases of transfer
m handshaking (greeting)
m transfer of messages
m closure
r command/response interaction
m commands,ASCII text
m response,status code and phrase
r messages must be in 7-bit ASCII
2,Application Layer 31
Sample smtp interaction
S,220 hamburger.edu
C,HELO crepes.fr
S,250 Hello crepes.fr,pleased to meet you
C,MAIL FROM,<alice@crepes.fr>
S,250 alice@crepes.fr..,Sender ok
C,RCPT TO,<bob@hamburger.edu>
S,250 bob@hamburger.edu,.,Recipient ok
C,DATA
S,354 Enter mail,end with "." on a line by itself
C,Do you like ketchup?
C,How about pickles?
C,,
S,250 Message accepted for delivery
C,QUIT
S,221 hamburger.edu closing connection
2,Application Layer 32
Try smtp interaction for yourself:
r telnet servername 25
r see 220 reply from server
r enter HELO,MAIL FROM,RCPT TO,DATA,QUIT
commands
above lets you send email without using email client
(reader)
2,Application Layer 33
smtp,final words
r smtp uses persistent
connections
r smtp requires message
(header & body) to be in 7-
bit ASCII
r certain character strings
not permitted in msg (e.g.,
CRLF.CRLF),Thus msg has
to be encoded (usually into
either base-64 or quoted
printable)
r smtp server uses
CRLF.CRLF to determine
end of message
Comparison with http:
r http,pull
r email,push
r both have ASCII
command/response
interaction,status codes
r http,each object
encapsulated in its own
response msg
r smtp,multiple objects sent
in multipart msg
2,Application Layer 34
Mail message format
smtp,protocol for exchanging
email msgs
RFC 822,standard for text
message format:
r header lines,e.g.,
m To:
m From:
m Subject:
different from smtp
commands!
r body
m the,message”,ASCII
characters only
header
body
blank
line
2,Application Layer 35
Message format,multimedia extensions
r MIME,multimedia mail extension,RFC 2045,2056
r additional lines in msg header declare MIME content
type
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
multimedia data
type,subtype,
parameter declaration
method used
to encode data
MIME version
encoded data
2,Application Layer 36
MIME types
Content-Type,type/subtype; parameters
Text
r example subtypes,plain,
html
Image
r example subtypes,jpeg,
gif
Audio
r exampe subtypes,basic
(8-bit mu-law encoded),
32kadpcm (32 kbps
coding)
Video
r example subtypes,mpeg,
quicktime
Application
r other data that must be
processed by reader
before,viewable”
r example subtypes,
msword,octet-stream
2,Application Layer 37
Multipart Type
From,alice@crepes.fr
To,bob@hamburger.edu
Subject,Picture of yummy crepe,
MIME-Version,1.0
Content-Type,multipart/mixed; boundary=98766789
--98766789
Content-Transfer-Encoding,quoted-printable
Content-Type,text/plain
Dear Bob,
Please find a picture of a crepe.
--98766789
Content-Transfer-Encoding,base64
Content-Type,image/jpeg
base64 encoded data,...,
........................,
......base64 encoded data
--98766789--
2,Application Layer 38
Mail access protocols
r SMTP,delivery/storage to receiver’s server
r Mail access protocol,retrieval from server
m POP,Post Office Protocol [RFC 1939]
? authorization (agent <-->server) and download
m IMAP,Internet Mail Access Protocol [RFC 1730]
? more features (more complex)
? manipulation of stored msgs on server
m HTTP,Hotmail,Yahoo! Mail,etc.
user
agent
sender’s mail
server
user
agent
SMTP SMTP POP3 or
IMAP
receiver’s mail
server
2,Application Layer 39
POP3 protocol
authorization phase
r client commands,
m user,declare username
m pass,password
r server responses
m +OK
m -ERR
transaction phase,client:
r list,list message numbers
r retr,retrieve message by
number
r dele,delete
r quit
C,list
S,1 498
S,2 912
S,,
C,retr 1
S,<message 1 contents>
S,,
C,dele 1
C,retr 2
S,<message 1 contents>
S,,
C,dele 2
C,quit
S,+OK POP3 server signing off
S,+OK POP3 server ready
C,user alice
S,+OK
C,pass hungry
S,+OK user successfully logged on
2,Application Layer 40
DNS,Domain Name System
People,many identifiers:
m SSN,name,passport #
Internet hosts,routers:
m IP address (32 bit) -
used for addressing
datagrams
m,name”,e.g.,
gaia.cs.umass.edu - used
by humans
Q,map between IP
addresses and name?
Domain Name System:
r distributed database
implemented in hierarchy of
many name servers
r application-layer protocol
host,routers,name servers to
communicate to resolve names
(address/name translation)
m note,core Internet
function,implemented as
application-layer protocol
m complexity at network’s
“edge”
2,Application Layer 41
DNS name servers
r no server has all name-
to-IP address mappings
local name servers:
m each ISP,company has
local (default) name server
m host DNS query first goes
to local name server
authoritative name server:
m for a host,stores that
host’s IP address,name
m can perform name/address
translation for that host’s
name
Why not centralize DNS?
r single point of failure
r traffic volume
r distant centralized
database
r maintenance
doesn’t scale!
2,Application Layer 42
DNS,Root name servers
r contacted by local name server that can not resolve name
r root name server:
m contacts authoritative name server if name mapping not known
m gets mapping
m returns mapping to local name server
b USC-ISI Marina del Rey,CA
l ICANN Marina del Rey,CA
e NASA Mt View,CA
f Internet Software C,Palo Alto,
CA
i NORDUnet Stockholm
k RIPE London
m WIDE Tokyo
a NSI Herndon,VA
c PSInet Herndon,VA
d U Maryland College Park,MD
g DISA Vienna,VA
h ARL Aberdeen,MD
j NSI (TBD) Herndon,VA
13 root name
servers worldwide
2,Application Layer 43
Simple DNS example
host surf.eurecom.fr
wants IP address of
gaia.cs.umass.edu
1,contacts its local DNS
server,dns.eurecom.fr
2,dns.eurecom.fr contacts
root name server,if
necessary
3,root name server contacts
authoritative name server,
dns.umass.edu,if
necessary requesting hostsurf.eurecom.fr gaia.cs.umass.edu
root name server
authorititive name server
dns.umass.edu
local name server
dns.eurecom.fr
1
2
3
4
5
6
2,Application Layer 44
DNS example
Root name server:
r may not know
authoritative name
server
r may know
intermediate name
server,who to
contact to find
authoritative name
server
requesting host
surf.eurecom.fr
gaia.cs.umass.edu
root name server
local name server
dns.eurecom.fr
1
2
3
4 5
6
authoritative name server
dns.cs.umass.edu
intermediate name server
dns.umass.edu
7
8
2,Application Layer 45
DNS,iterated queries
recursive query:
r puts burden of name
resolution on
contacted name
server
r heavy load?
iterated query:
r contacted server
replies with name of
server to contact
r,I don’t know this
name,but ask this
server”
requesting host
surf.eurecom.fr
gaia.cs.umass.edu
root name server
local name server
dns.eurecom.fr
1
2
3
4
5 6
authoritative name server
dns.cs.umass.edu
intermediate name server
dns.umass.edu
7
8
iterated query
2,Application Layer 46
DNS,caching and updating records
r once (any) name server learns mapping,it caches
mapping
m cache entries timeout (disappear) after some
time
r update/notify mechanisms under design by IETF
m RFC 2136
m http://www.ietf.org/html.charters/dnsind-charter.html
2,Application Layer 47
DNS records
DNS,distributed db storing resource records (RR)
r Type=NS
m name is domain (e.g,
foo.com)
m value is IP address of
authoritative name
server for this domain
RR format,(name,value,type,ttl)
r Type=A
m name is hostname
m value is IP address
r Type=CNAME
m name is alias name for some
“cannonical” (the real) name
www.ibm.com is really
servereast.backup2.ibm.com
m value is cannonical name
r Type=MX
m value is name of mailserver
associated with name
2,Application Layer 48
DNS protocol,messages
DNS protocol, query and reply messages,both with
same message format
msg header
r identification,16 bit #
for query,reply to query
uses same #
r flags:
m query or reply
m recursion desired
m recursion available
m reply is authoritative
2,Application Layer 49
DNS protocol,messages
Name,type fields
for a query
RRs in reponse
to query
records for
authoritative servers
additional,helpful”
info that may be used
2,Application Layer 50
Socket programming
Socket API
r introduced in BSD4.1 UNIX,
1981
r explicitly created,used,
released by apps
r client/server paradigm
r two types of transport
service via socket API,
m unreliable datagram
m reliable,byte stream-
oriented
a host-local,application-
created/owned,
OS-controlled interface
(a,door”) into which
application process can
both send and
receive messages to/from
another (remote or
local) application process
socket
Goal,learn how to build client/server application that
communicate using sockets
2,Application Layer 51
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
process
TCP with
buffers,
variables
socket
controlled by
application
developer
controlled by
operating
system
host or
server
process
TCP with
buffers,
variables
socket
controlled by
application
developer
controlled by
operating
system
host or
server
internet
2,Application Layer 52
Socket programming with TCP
Client must contact server
r server process must first
be running
r server must have created
socket (door) that
welcomes client’s contact
Client contacts server by:
r creating client-local TCP
socket
r specifying IP address,port
number of server process
r When client creates socket,
client TCP establishes
connection to server TCP
r When contacted by client,
server TCP creates new
socket for server process to
communicate with client
m allows server to talk with
multiple clients
TCP provides reliable,in-order
transfer of bytes (“pipe”)
between client and server
application viewpoint
2,Application Layer 53
Socket programming with TCP
Example client-server app:
r client reads line from
standard input (inFromUser
stream),sends to server via
socket (outToServer
stream)
r server reads line from socket
r server converts line to
uppercase,sends back to
client
r client reads,prints modified
line from socket
(inFromServer stream)
o
u
t
T
o
S
e
r
v
e
r
t o n e t w o r k f r o m n e t w o r k
i
n
F
r
o
m
S
e
r
v
e
r
i
n
F
r
o
m
U
s
e
r
k e y b o a r d m o n i t o r
P r o c e s s
c l i e n t S o c k e t
i n p u t
s t r e a m
i n p u t
s t r e a m
o u t p u t
s t r e a m
T C P
s o c k e t
Input stream,
sequence of bytes
into processoutput stream,
sequence of bytes
out of process
Client
process
client TCP
socket
2,Application Layer 54
Client/server socket interaction,TCP
wait for incoming
connection request
connectionSocket =
welcomeSocket.accept()
create socket,
port=x,for
incoming request:
welcomeSocket =
ServerSocket()
create socket,
connect to hostid,port=x
clientSocket =
Socket()
close
connectionSocket
read reply from
clientSocket
close
clientSocket
Server (running on hostid) Client
send request using
clientSocketread request from
connectionSocket
write reply to
connectionSocket
TCP
connection setup
2,Application Layer 55
Example,Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname",6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
2,Application Layer 56
Example,Java client (TCP),cont.
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER," + modifiedSentence);
clientSocket.close();
}
}
Create
input stream
attached to socket
Send line
to server
Read line
from server
2,Application Layer 57
Example,Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
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()));
Create
welcoming socket
at port 6789
Wait,on welcoming
socket for contact
by client
Create input
stream,attached
to socket
2,Application Layer 58
Example,Java server (TCP),cont
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Read in line
from socket
Create output
stream,attached
to socket
Write out line
to socket
End of while loop,
loop back and wait for
another client connection
2,Application Layer 59
Socket programming with UDP
UDP,no,connection” between
client and server
r no handshaking
r sender explicitly attaches
IP address and port of
destination
r server must extract IP
address,port of sender
from received datagram
UDP,transmitted data may be
received out of order,or
lost
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
2,Application Layer 60
Client/server socket interaction,UDP
close
clientSocket
Server (running on hostid)
read reply from
clientSocket
create socket,
clientSocket =
DatagramSocket()
Client
Create,address (hostid,port=x,
send datagram request
using clientSocket
create socket,
port=x,for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port umber
2,Application Layer 61
Example,Java client (UDP)
s
e
n
d
P
a
c
k
e
t
t o n e t w o r k f r o m n e t w o r k
r
e
c
e
i
v
e
P
a
c
k
e
t
i
n
F
r
o
m
U
s
e
r
k e y b o a r d m o n i t o r
P r o c e s s
c l i e n t S o c k e t
U D P
p a c k e t
i n p u t
s t r e a m
U D P
p a c k e t
U D P
s o c k e t
Output,sends
packet (TCP sent
“byte stream”)
Input,receives
packet (TCP
received,byte
stream”)
Client
process
client UDP
socket
2,Application Layer 62
Example,Java client (UDP)
import java.io.*;
import java.net.*;
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();
Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS
2,Application Layer 63
Example,Java client (UDP),cont.
DatagramPacket sendPacket =
new DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Create datagram
with data-to-send,
length,IP addr,port
Send datagram
to server
Read datagram
from server
2,Application Layer 64
Example,Java server (UDP)
import java.io.*;
import java.net.*;
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)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
Create
datagram socket
at port 9876
Create space for
received datagram
Receive
datagram
2,Application Layer 65
Example,Java server (UDP),cont
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData,sendData.length,IPAddress,
port);
serverSocket.send(sendPacket);
}
}
}
Get IP addr
port #,of
sender
Write out
datagram
to socket
End of while loop,
loop back and wait for
another datagram
Create datagram
to send to client
2,Application Layer 66
Socket programming,references
C-language tutorial (audio/slides):
r,Unix Network Programming” (J,Kurose),
http://manic.cs.umass.edu/~amldemo/courseware/intro.
Java-tutorials:
r,All About Sockets” (Sun tutorial),
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-
sockets.html
r,Socket Programming in Java,a tutorial,”
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-
sockets.html
2,Application Layer 67
Part 2,Summary
r application service
requirements:
m reliability,bandwidth,
delay
r client-server paradigm
r Internet transport
service model
m connection-oriented,
reliable,TCP
m unreliable,datagrams,
UDP
Our study of network apps now complete!
r specific protocols:
m http
m ftp
m smtp,pop3
m dns
r socket programming
m client/server
implementation
m using tcp,udp sockets
2,Application Layer 68
Part 2,Summary
r typical request/reply
message exchange:
m client requests info or
service
m server responds with
data,status code
r message formats:
m headers,fields giving
info about data
m data,info being
communicated
Most importantly,learned about protocols
r control vs,data msgs
m in-based,out-of-band
r centralized vs,decentralized
r stateless vs,stateful
r reliable vs,unreliable msg
transfer
r,complexity at network
edge”
r security,authentication