Professional Documents
Culture Documents
Application Layer
2: Application Layer 2
Chapter 2: Application Layer
Our goals: learn about protocols by
conceptual, examining popular
implementation application-level
aspects of network protocols
application protocols HTTP
transport-layer FTP
service models SMTP / POP3 / IMAP
client-server
DNS
programming network
paradigm
applications
peer-to-peer
paradigm
socket API
2: Application Layer 3
Some network apps
E-mail Internet telephone
Web Real-time video conference
Instant messaging Massive parallel computing
Remote login
P2P file sharing
Multi-user network
games
Streaming stored
video clips
2: Application Layer 4
Creating a network app
Write programs that application
transport
systems and
physical
communicate over a
network.
e.g., Web: Web server
software communicates
with browser software
No software written for application
2: Application Layer 6
Application architectures
Client-server
Peer-to-peer (P2P)
Hybrid of client-server and P2P
2: Application Layer 7
Client-server archicture
server:
always-on host
permanent IP address
server farms for scaling
clients:
communicate with
server
may be intermittently
connected
may have dynamic IP
addresses
do not communicate
directly with each other
2: Application Layer 8
Pure P2P architecture
no always on server
arbitrary end systems
directly communicate
peers are intermittently
connected and change IP
addresses
Highly scalable
2: Application Layer 9
Hybrid of client-server and P2P
Napster
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 10
Processes communicating
Process: program running Client process: process that
within a host. initiates communication
within same host, two Server process: process that
processes communicate waits to be contacted
using inter-process
communication (defined
by OS).
processes in different Note: applications with
hosts communicate by P2P architectures have
exchanging messages client processes &
server processes
2: Application Layer 11
Sockets
host or host or
process sends/receives server server
messages to/from its
socket controlled by
app developer
socket analogous to door process process
2: Application Layer 13
Transport service requirements of common apps
2: Application Layer 14
Internet transport protocols services
Application Underlying
Application layer protocol transport protocol
2: Application Layer 16
Addressing processes
For a process to receive Identifier includes
messages, it must have an both the IP address
identifier and port numbers
A host has a unique32-bit IP associated with the
address process on the host.
Q: does the IP address of the Example port numbers:
host on which the process HTTP server: 80
runs suffice for identifying Mail server: 25
the process? www.iana.org
Answer: No, many processes
can be running on same
host
2: Application Layer 17
App-layer protocol defines
Types of messages Public-domain protocols:
exchanged, eg, request defined in RFCs
& response messages allows for interoperability
Syntax of message eg, HTTP, SMTP
types: what fields in Proprietary protocols:
messages & how fields eg, KaZaA
are delineated
Semantics of the
fields, ie, meaning of
information in fields
Rules for when and
how processes send &
respond to messages 2: Application Layer 18
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
app architectures TCP
app requirements 2.8 Socket programming with
2.2 Web and HTTP UDP
2.4 Electronic Mail 2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
2: Application Layer 19
Web and HTTP
History
On demand, Publishing
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:
path name
host name
www.someschool.edu/someDept/pic.gif
2: Application Layer 20
HTTP overview
HTTP: hypertext
transfer protocol HT
TP
r
equ
Webs application layer PC running HT est
TP
protocol Explorer res
pon
se
client/server model
client: browser that
st
requests, receives, q ue
e
TPr on se Server
displays Web objects HT r es
p running
TP Apache Web
server: Web server HT
server
sends objects in
response to requests
Mac running
HTTP 1.0: RFC 1945 Navigator
HTTP 1.1: RFC 2616
2: Application Layer 21
HTTP overview (continued)
Uses TCP: HTTP is stateless
client initiates TCP server maintains no
connection (creates socket) information about past
to server, port 80 client requests
server accepts TCP
connection from client aside
Protocols that maintain
HTTP messages (application-
state are complex!
layer protocol messages) past history (state) must
exchanged between browser
be maintained
(HTTP client) and Web
if server/client crashes,
server (HTTP server)
their views of state may
TCP connection closed
be inconsistent, must be
reconciled
2: Application Layer 22
HTTP connections
C&S commu- extended time Nonpersistent HTTP
At most one object is sent over a
Client making a series of
TCP connection.
requests
HTTP/1.0 uses nonpersistent
Server- responding to each HTTP
request Persistent HTTP
Designer- should Multiple objects can be sent over
request /response sent single TCP connection between
over client and server.
Separate connection? HTTP/1.1 uses persistent
connections in default mode
2: Application Layer 23
Nonpersistent HTTP
(contains text,
Suppose user enters URL references to 10
www.someSchool.edu/someDepartment/home.index jpeg images)
time
2: Application Layer 24
Nonpersistent HTTP (cont.)
2: Application Layer 27
HTTP request message
request line
(GET, POST, GET /somedir/page.html HTTP/1.1
HEAD commands) Host: www.someschool.edu
User-agent: Mozilla/4.0
header Connection: close
lines Accept-language:fr
Carriage return,
(extra carriage return, line feed)
line feed
indicates end
of message
2: Application Layer 28
HTTP request message: general format
2: Application Layer 29
Uploading form input
Post method:
Web page often
includes form input URL method:
Input is uploaded to Uses GET method
server in entity body Input is uploaded in URL
field of request line:
www.somesite.com/animalsearch?monkeys&banana
2: Application Layer 30
Method types
HTTP/1.0 HTTP/1.1
GET GET, POST, HEAD
POST PUT
uploads file in entity
HEAD
body to path specified
asks server to leave in URL field
requested object out of DELETE
response
deletes file specified in
the URL field
2: Application Layer 31
HTTP response message
status line
(protocol
status code HTTP/1.1 200 OK
status phrase) Connection close
Date: Thu, 06 Aug 2016 12:00:15 GMT
header Server: Apache/1.3.0 (Unix)
lines Last-Modified: Mon, 22 Jun 2015 ...
Content-Length: 6821
Content-Type: text/html
2: Application Layer 32
HTTP response status codes
In first line in server->client response message.
A few sample codes:
200 OK
request succeeded, requested object later in this message
301 Moved Permanently
requested object moved, new location specified later in
this message (Location:)
400 Bad Request
request message not understood by server
404 Not Found
requested document not found on this server
505 HTTP Version Not Supported
2: Application Layer 33
Trying out HTTP (client side) for yourself
2: Application Layer 34
User-server state:cookies(RFC 2965)
Many major Web sites Example:
use cookies Susan access Internet
Four components: always from same PC
She visits a specific e-
1) cookie header line in
commerce site for first
the HTTP response
time
message
When initial HTTP
2) cookie header line in
requests arrives at site,
HTTP request message
site creates a unique ID
3) cookie file kept on and creates an entry in
users host and managed backend database for
by users browser ID
4) back-end database at
Web site
2: Application Layer 35
Cookies: keeping state (cont.)
client server
Cookie file usual http request msg server n e
da try i
t ab n b
usual http response + creates ID as ac
e ke
ebay: 8734 Set-cookie: 1678 1678 for user nd
Cookie file
usual http request msg
amazon: 1678 cookie: 1678 cookie- ss
ebay: 8734 specific acce
usual http response msg action
s
one week later:
s
ce
ac
usual http request msg
Cookie file cookie-
cookie: 1678
amazon: 1678 spectific
ebay: 8734 usual http response msg action
2: Application Layer 36
Cookies (continued)
aside
What cookies can bring: Cookies and privacy:
authorization cookies permit sites to
shopping carts learn a lot about you
recommendations you may supply name
and e-mail to sites
user session state
search engines use
(Web e-mail)
redirection & cookies
to learn yet more
advertising companies
obtain info across
sites
2: Application Layer 37
Web caches (proxy server)
Goal: satisfy client request without involving origin server
client
origin
server
2: Application Layer 38
More about Web caching
Cache acts as both client Why Web caching?
and server Reduce response time for client
Typically cache is installed request.
by ISP (university, Reduce traffic on an institutions
company, residential ISP) access link, Up gradation to
higher link can be postponed
Internet dense with caches enables
poor content providers to
effectively deliver content, thus
reducing web traffic in Internet
as whole
2: Application Layer 39
Caching example
origin
Assumptions
servers
average object size =
100,0000 bits public
Internet
avg. request rate from
institutions browsers to
origin servers = 15/sec
delay from institutional router 15 Mbps
access link
to any origin server and back
to router = 2 sec institutional
network
Consequences 100 Mbps LAN
utilization on LAN = 15%
utilization on access link = 100%
total delay = Internet delay + institutional
access delay + LAN delay cache
= 2 sec + minutes + milliseconds
2: Application Layer 40
Caching example (cont)
origin
Possible solution
servers
increase bandwidth of access
link to, say, 100 Mbps public
Internet
Consequences
utilization on LAN = 15%
utilization on access link = 15%
Total delay = Internet delay + 15 Mbps
access link
access delay + LAN delay
= 2 sec + msecs + msecs institutional
network
often a costly upgrade 100 Mbps LAN
institutional
cache
2: Application Layer 41
Caching example (cont)
origin
servers
Install cache
suppose hit rate is .4 public
Consequence Internet
40% requests will be
satisfied almost immediately
60% requests satisfied by
15 Mbps
origin server
access link
utilization of access link
reduced to 60%, resulting in institutional
negligible delays (say 10 network
100 Mbps LAN
msec)
total avg delay = Internet
delay + access delay + LAN
delay = .4*.01+ .6*(2.01)
secs + milliseconds < 1.4 institutional
secs cache
2: Application Layer 42
Conditional GET
2: Application Layer 44
FTP: the file transfer protocol
2: Application Layer 45
FTP: separate control, data connections
TCP control connection
FTP client contacts FTP port 21
server at port 21, specifying
TCP as transport protocol
TCP data connection
Client obtains authorization FTP port 20 FTP
over control connection client server
Client browses remote
Server opens a second TCP
directory by sending
commands over control data connection to transfer
connection. another file.
Control connection: out of
When server receives a
command for a file transfer, band
the server opens a TCP data FTP server maintains state:
connection to client current directory, earlier
After transferring one file, authentication
server closes connection.
2: Application Layer 46
FTP commands, responses
2: Application Layer 47
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
2.2 Web and HTTP TCP
2.3 FTP 2.8 Socket programming with
2.4 Electronic Mail
UDP
2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
2: Application Layer 48
Electronic Mail outgoing
message queue
user mailbox
user
Three major components: agent
user agents mail
user
mail servers server
agent
simple mail transfer SMTP mail
protocol: SMTP server user
User Agent SMTP agent
2: Application Layer 49
Electronic Mail: mail servers
user
Mail Servers agent
mailbox contains incoming
mail
user
messages for user server
agent
message queue of outgoing
(to be sent) mail messages
SMTP mail
server user
SMTP protocol between mail
servers to send email SMTP agent
messages
SMTP
client: sending mail user
mail
server server agent
2: Application Layer 50
Electronic Mail: SMTP [RFC 5321/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
2: Application Layer 51
Scenario: Alisha sends message to
Bob
1) Alisha uses UA to compose 4) SMTP client sends Alishas
message and to message over the TCP
bob@someschool.edu connection
2) Alishas UA sends message 5) Bobs mail server places the
to her mail server; message message in Bobs mailbox
placed in message queue 6) Bob invokes his user agent to
read message
3) Client side of SMTP opens
TCP connection with Bobs
mail server
1 mail
mail
server user
user server
2 agent
agent 3 6
4 5
2: Application Layer 52
Sample SMTP interaction
S: 220 hamburger.edu
C: HELO crepes.fr
S: 250 Hello crepes.fr, pleased to meet you
C: MAIL FROM: <Alisha@crepes.fr>
S: 250 Alisha@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 53
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 54
SMTP: final words
SMTP uses persistent Comparison with HTTP:
connections
HTTP: pull
SMTP requires message
SMTP: push
(header & body) to be in 7-
bit ASCII both have ASCII
SMTP server uses command/response interaction,
CRLF.CRLF to determine status codes
end of message HTTP: each object encapsulated in
its own response msg
SMTP: multiple objects sent in
multipart msg
2: Application Layer 55
Mail message format
2: Application Layer 56
Message format: multimedia extensions
MIME: Multipurpose Internet Mail Extensions multimedia mail
extension, RFC 2045, 2056
additional lines in msg header declare MIME content type
From: Alisha@crepes.fr
MIME version To: bob@hamburger.edu
Subject: Picture of yummy crepe.
method used MIME-Version: 1.0
to encode data Content-Transfer-Encoding: base64
Content-Type: image/jpeg
multimedia data
type, subtype, base64 encoded data .....
parameter declaration .........................
......base64 encoded data
encoded data
2: Application Layer 57
Mail access protocols
SMTP SMTP access user
user
agent protocol agent
2: Application Layer 58
POP3 protocol,
RFC1939 S:
C:
+OK POP3 server ready
user bob
authorization phase S: +OK
C: pass hungry
client commands:
S: +OK user successfully logged on
user: declare username
C: list
pass: password
S: 1 498
server responses S: 2 912
+OK S: .
C: retr 1
-ERR
S: <message 1 contents>
transaction phase, client: S: .
list: list message numbers C: dele 1
C: retr 2
retr: retrieve message by S: <message 1 contents>
number S: .
dele: delete C: dele 2
quit C: quit
S: +OK POP3 server signing off
2: Application Layer 59
POP3 (more) and IMAP
More about POP3 IMAP
Previous example uses Keep all messages in one place:
download and delete the server
mode. Allows user to organize messages
Bob cannot re-read e- in folders
mail if he changes Obtain component of message
client IMAP keeps user state across
Download-and-keep: sessions:
names of folders and
copies of messages on
mappings between message
different clients IDs and folder name
POP3 is stateless
across sessions
2: Application Layer 60
Web based email
User agent
Web browser
User communicates
with mailbox through
HTTP
Email is sent through
HTTP
2: Application Layer 61
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
2.2 Web and HTTP TCP
2.3 FTP 2.8 Socket programming with
2.4 Electronic Mail
UDP
2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
2: Application Layer 62
DNS: Domain Name System
2: Application Layer 64
Distributed, Hierarchical Database
Root DNS Servers
2: Application Layer 65
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
k RIPE London (also Amsterdam,
g US DoD Vienna, VA
Frankfurt) Stockholm (plus 3
i Autonomica,
h ARL Aberdeen, MD
j Verisign, ( 11 locations) other locations)
m WIDE Tokyo
e NASA Mt View, CA
f Internet Software C. Palo Alto,
CA (and 17 other locations)
13 root name
servers worldwide
b USC-ISI Marina del Rey, CA
l ICANN Los Angeles, CA
2: Application Layer 66
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 67
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 68
Example root DNS server
2
Host at cis.poly.edu 3
TLD DNS server
wants IP address for 4
gaia.cs.umass.edu
5
2: Application Layer 69
Recursive queries root DNS server
recursive query:
puts burden of name 2 3
resolution on 6
7
contacted name
TLD DNS serve
server
heavy load?
local DNS server
dns.poly.edu 5 4
1 8
gaia.cs.umass.edu
2: Application Layer 70
DNS: caching and updating records
once (any) name server learns mapping, it caches
mapping
cache entries timeout (disappear) after some
time
TLD servers typically cached in local name
servers
Thus root name servers not often visited
update/notify mechanisms under design by IETF
RFC 2136
http://www.ietf.org/html.charters/dnsind-charter.html
2: Application Layer 71
DNS records
DNS: distributed db storing resource records (RR)
RR format: (name, value, type, ttl)
Type=A Type=CNAME
name is hostname name is alias name for some
value is IP address cannonical (the real) name
www.ibm.com is really
Type=NS servereast.backup2.ibm.com
name is domain (e.g. value is cannonical name
foo.com)
value is IP address of Type=MX
authoritative name
value is name of mailserver
server for this domain
associated with name
2: Application Layer 72
DNS protocol, messages
DNS protocol : query and reply messages, both with
same message format
msg header
identification: 16 bit #
for query, reply to query
uses same #
flags:
query or reply
recursion desired
recursion available
reply is authoritative
2: Application Layer 73
DNS protocol, messages
RRs in reponse
to query
records for
authoritative servers
additional helpful
info that may be used
Try -nslookup
2: Application Layer 74
Inserting records into DNS
Example: just created startup Network Utopia
Register name networkuptopia.com at a registrar
(e.g., Network Solutions, ICANN, 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:
2: Application Layer 75
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
app architectures TCP
app requirements 2.8 Socket programming with
2.2 Web and HTTP UDP
2.4 Electronic Mail 2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
2: Application Layer 76
P2P file sharing
Alisha chooses one of the
Example peers, Bob.
Alisha runs P2P client
File is copied from Bobs PC
to Alishas notebook:
application on her
HTTP
notebook computer
While Alisha downloads,
Intermittently
other users uploading
connects to Internet; from Alisha.
gets new IP address Alishas peer is both a Web
for each connection client and a transient Web
Asks for Hey Jude server.
Application displays All peers are servers = highly
other peers that have scalable!
copy of Hey Jude.
2: Application Layer 77
P2P: centralized directory
Bob
original Napster design centralized
1) when peer connects, it directory server
1
informs central server: peers
IP address 1
content
1 3
2) Alisha queries for Hey
Jude 2 1
Alisha
2: Application Layer 78
P2P: problems with centralized directory
2: Application Layer 79
BitTorrent
2: Application Layer 80
Query flooding: Gnutella
fully distributed overlay network: graph
no central server edge between peer X and Y if
public domain protocol theres a TCP connection
many Gnutella clients all active peers and edges is
implementing protocol overlay net
Edge is not a physical link
Given peer will typically be
connected with < 10
overlay neighbors
2: Application Layer 81
Gnutella: protocol
File transfer:
Query message
HTTP
sent over existing TCP
connections
Query
peers forward
QueryHit
Query message
QueryHit ery Qu
ery
Qu Hit
sent over ery
Qu
reverse
Query
path
QueryHit
Scalability: Qu
er
y
limited scope
flooding
2: Application Layer 82
Gnutella: Peer joining
1. Joining peer X must find some other peer in
Gnutella network: use list of candidate peers
2. X sequentially attempts to make TCP with peers
on list until connection setup with Y
3. X sends Ping message to Y; Y forwards Ping
message.
4. All peers receiving Ping message respond with
Pong message
5. X receives many Pong messages. It can then
setup additional TCP connections
Peer leaving: see homework problem!
2: Application Layer 83
Exploiting heterogeneity: KaZaA
children. 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 84
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 85
Kazaa tricks
Limitations on simultaneous uploads
Request queuing
Incentive priorities
Parallel downloading
2: Application Layer 86
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
2.2 Web and HTTP TCP
2.3 FTP 2.8 Socket programming with
2.4 Electronic Mail
UDP
2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
2: Application Layer 87
Socket programming
Network applications
Pair of Program - C/S
Processes and Communicate thru
Sockets
Two types of Net App
RFC based, Propriety
2: Application Layer 88
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
2: Application Layer 89
Socket-programming using TCP
Socket: a door between application process and end-
end-transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
controlled by process application
application process
developer
developer socket socket
TCP with TCP with controlled by
controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
2: Application Layer 90
Socket programming with TCP
Client must contact server When contacted by client, server
server process must first TCP creates new socket for
be running server process to communicate
server must have created with client
socket (door) that allows server to talk with
welcomes clients contact multiple clients
source port numbers
Client contacts server by:
creating client-local TCP
used to distinguish
clients
socket
(draw figure)
specifying IP address, port
application viewpoint
number of server process
When client creates TCP provides reliable, in-order
socket: client TCP transfer of bytes (pipe)
establishes connection to between client and server
server TCP . 3 Way
Handshake 2: Application Layer 91
Stream jargon
A stream is a sequence of
characters that flow into
or out of a process.
An input stream is
attached to some input
source for the process, eg,
keyboard or socket.
An output stream is
attached to an output
source, eg, monitor or
socket.
2: Application Layer 92
Socket programming with TCP
keyboard monitor
inFromUser
input
stream
stream) , sends to server via Client
socket (outToServer Process
process
stream)
2) server reads line from socket
3) server converts line to
uppercase, sends back to
inFromServer
outToServer
client
output input
stream stream
2: Application Layer 93
Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
2: Application Layer 94
Example: Java client (TCP)
import java.io.*; Java Package, contains classes
import java.net.*; to create streams, other for net supp
class TCPClient { defines variables,methods
sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');
clientSocket.close();
}
}
2: Application Layer 96
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
2: Application Layer 97
Example: Java server (TCP), cont
Create output
stream, attached DataOutputStream outToClient =
to socket new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket clientSentence = inFromClient.readLine();
2: Application Layer 98
Transport Service Primitives
6-6-1
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number close
clientSocket
inFromUser
input
stream
Client
Process
Input: receives
process
packet (TCP
Output: sends received byte
packet (TCP sent stream)
receivePacket
sendPacket
byte stream) UDP UDP
packet packet
client
clientSocket UDP
socket UDP
socket
class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Translate
InetAddress IPAddress = InetAddress.getByName("hostname");
hostname to IP
address using DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
class UDPServer {
public static void main(String args[]) throws Exception
Create {
datagram socket
DatagramSocket serverSocket = new DatagramSocket(9876);
at port 9876
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
DatagramPacket receivePacket =
received datagram
new DatagramPacket(receiveData, receiveData.length);
Receive serverSocket.receive(receivePacket);
datagram
2: Application Layer 109
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
InetAddress IPAddress = receivePacket.getAddress();
port #, of
sender int port = receivePacket.getPort();
sendData = capitalizedSentence.getBytes();
Create datagram
DatagramPacket sendPacket =
to send to client new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram serverSocket.send(sendPacket);
to socket }
}
} End of while loop,
loop back and wait for
another datagram
2: Application Layer 110
Chapter 2: Application layer
2.1 Principles of 2.6 P2P file sharing
network applications 2.7 Socket programming with
app architectures TCP
app requirements 2.8 Socket programming with
2.2 Web and HTTP UDP
2.4 Electronic Mail 2.9 Building a Web server
SMTP, POP3, IMAP
2.5 DNS
P2P FTP