CN_Module-5_Application Layer
CN_Module-5_Application Layer
Application Layer
By
2: Application Layer 1
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
2
Some network apps
e-mail voice over IP
web real-time video
instant messaging conferencing
remote login Social Networking
multi-user network
games
streaming stored video
clips
3
Creating a network app application
transport
network
data link
4
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
5
Application architectures
Client-server
Peer-to-peer (P2P)
Hybrid of client-server and P2P
6
Client-server architecture
server:
❖ always-on host
❖ permanent IP address
❖ server farms for
scaling(Data Center)
clients:
client/server ❖ communicate with server
❖ may be intermittently
connected
❖ may have dynamic IP
addresses
❖ do not communicate
directly with each other
7
Pure P2P architecture
no always-on server
arbitrary end systems
directly communicate peer-peer
peers are intermittently
connected and change IP
addresses
8
Hybrid of client-server and P2P
Skype
❖ voice-over-IP P2P application
❖ centralized server: finding address of remote
party:
❖ client-client connection: direct (not through
server)
Instant messaging
❖ chatting between two users is P2P
❖ centralized service: client presence
detection/location
• user registers its IP address with central
server when it comes online
• user contacts central server to find IP
addresses of buddies
9
Processes communicating
Process: program running Client process: process
within a host. that initiates
within same host, two
communication
processes communicate Server process: process
using inter-process that waits to be
communication (defined contacted
by OS).
processes in different Note: applications with
hosts communicate by P2P architectures have
exchanging messages client processes &
server processes
10
Sockets
process sends/receives
host or host or
server server
messages to/from its
socket controlled by
app developer
socket analogous to door process process
12
Addressing processes
to receive messages, identifier includes both
process must have IP address and port
identifier numbers associated with
host device has unique process on host.
32-bit IP address Example port numbers:
Q: does IP address of ❖ HTTP server: 80
host on which process ❖ Mail server: 25
runs suffice for to send HTTP message
identifying the to gaia.cs.umass.edu web
process? server:
❖ A: No, many ❖ IP address: 128.119.245.12
processes can be ❖ Port number: 80
running on same host
13
What transport service does an app need?
Data loss Throughput
some apps (e.g., audio) can some apps (e.g.,
tolerate some loss multimedia) require
other apps (e.g., file minimum amount of
transfer, telnet) require throughput to be
100% reliable data “effective”
transfer
other apps (“elastic apps”)
Timing make use of whatever
some apps (e.g., throughput they get
Internet telephony,
interactive games) Security
require low delay to be Encryption, data
“effective” integrity, …
14
Transport service requirements of common apps
15
Internet transport protocols services
Application Underlying
Application layer protocol transport protocol
17
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
18
Web and HTTP
First some jargon
Web page consists of objects
Object can be HTML file, JPEG image, Java
applet, audio file,…
Web page consists of base HTML-file which
includes several referenced objects
Each object is addressable by a URL
Example URL:
www.someschool.edu/someDept/pic.gif
19
HTTP overview
HTTP: hypertext
transfer protocol
Web’s application layer PC running
protocol Explorer
client/server model
❖ client: browser that
requests, receives, Server
“displays” Web objects running
Apache Web
❖ server: Web server
server
sends objects in
response to requests
Mac running
Navigator
20
HTTP overview (continued)
Uses TCP: HTTP is “stateless”
client initiates TCP server maintains no
connection (creates socket) information about
to server, port 80 past client requests
server accepts TCP
connection from client aside
Protocols that maintain
HTTP messages (application- “state” are complex!
layer protocol messages) past history (state) must
exchanged between browser be maintained
(HTTP client) and Web
server (HTTP server)
TCP connection closed
21
HTTP connections
Nonpersistent HTTP Persistent HTTP
At most one object is Multiple objects can
sent over a TCP be sent over single
connection. TCP connection
between client and
server.
22
Nonpersistent HTTP
(contains text,
Suppose user enters URL references to 10
www.someSchool.edu/someDepartment/home.index jpeg images)
time
23
Nonpersistent HTTP (cont.)
24
Non-Persistent HTTP: Response time
Definition of RTT: time for
a small packet to travel
from client to server
and back. initiate TCP
connection
Response time: RTT
one RTT to initiate TCP request
file
connection time to
RTT
transmit
one RTT for HTTP file
request and first few file
received
bytes of HTTP response
to return time time
file transmission time
total = 2RTT+transmit time
25
Persistent HTTP
26
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,
line feed (extra carriage return, line feed)
indicates end
of message
27
HTTP request message: general format
28
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
29
Method types HTTP/1.1
DELETE
HTTP/1.0 ❖ deletes file specified in the
URL field
GET
POST TRACE
HEAD ❖ Echoes request msg
❖ asks server to leave from server
requested object out of OPTIONS
response
❖ Returns HTTP methods
HTTP/1.1
that the server supports
GET, POST, HEAD
CONNECT
PUT
❖ TCP/IP tunnel for HTTP
❖ uploads file in entity
body to path specified
in URL field 30
HTTP response message
status line
(protocol
status code HTTP/1.1 200 OK
status phrase) Connection close
Date: Thu, 06 Aug 1998 12:00:15 GMT
header Server: Apache/1.3.0 (Unix)
lines Last-Modified: Mon, 22 Jun 1998 …...
Content-Length: 6821
Content-Type: text/html
31
HTTP response status codes
In first line in server->client response message.
A few sample codes:
200 OK
❖ request succeeded, requested object later in this message
301 Moved Permanently
❖ requested object moved, new location specified later in
this message (Location:)
400 Bad Request
❖ request message not understood by server
404 Not Found
❖ requested document not found on this server
505 HTTP Version Not Supported
32
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
33
FTP: the file transfer protocol
34
FTP: separate control, data connections
TCP control connection
FTP client contacts FTP server port 21
at port 21, TCP is transport
protocol TCP data connection
client authorized over control FTP port 20 FTP
connection client server
client browses remote
server opens another TCP
directory by sending commands
data connection to transfer
over control connection.
another file.
when server receives file
control connection: “out of
transfer command, server
band”
opens 2nd TCP connection (for
file) to client FTP server maintains “state”:
current directory, earlier
after transferring one file,
authentication
server closes data connection.
35
FTP commands, responses
36
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
37
Electronic Mail outgoing
message queue
user mailbox
user
Three major components: agent
user agents mail
user
server
mail servers agent
simple mail transfer SMTP mail
protocol: SMTP server user
SMTP agent
User Agent
a.k.a. “mail reader” SMTP
mail user
composing, editing, reading agent
server
mail messages
e.g., Eudora, Outlook, elm, user
Mozilla Thunderbird agent
user
outgoing, incoming messages agent
stored on server
38
Electronic Mail: mail servers
user
Mail Servers agent
mailbox contains incoming mail
user
messages for user server
agent
message queue of outgoing
SMTP
(to be sent) mail messages mail
server user
SMTP protocol between mail
servers to send email SMTP agent
messages SMTP
❖ client: sending mail mail user
agent
server server
39
Electronic Mail: SMTP [RFC 2821]
40
Scenario: Alice sends message to Bob
1) Alice uses UA to compose 4) SMTP client sends Alice’s
message and “to” message over the TCP
[email protected] connection
2) Alice’s UA sends message 5) Bob’s mail server places the
to her mail server; message message in Bob’s mailbox
placed in message queue 6) Bob invokes his user agent
3) Client side of SMTP opens to read message
TCP connection with Bob’s
mail server
1 mail
mail
server user
user server
2 agent
agent 3 6
4 5
41
Sample SMTP interaction
S: 220 hamburger.edu
C: HELO crepes.fr
S: 250 Hello crepes.fr, pleased to meet you
C: MAIL FROM: <[email protected]>
S: 250 [email protected]... Sender ok
C: RCPT TO: <[email protected]>
S: 250 [email protected] ... 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
42
SMTP: final words
SMTP uses persistent Comparison with HTTP:
connections
HTTP: pull
SMTP requires message
(header & body) to be in 7- SMTP: push
bit ASCII both have ASCII
SMTP server uses command/response
CRLF.CRLF to determine interaction, status codes
end of message
HTTP: each object
encapsulated in its own
response msg
SMTP: multiple objects
sent in multipart msg
43
Mail message format
44
Mail access protocols
SMTP SMTP access user
user
agent protocol agent
45
POP3 protocol S: +OK POP3 server ready
C: user bob
authorization phase S: +OK
C: pass hungry
client commands: S: +OK user successfully logged on
❖ user: declare username
C: list
❖ pass: password S: 1 498
server responses S: 2 912
S: .
❖ +OK
C: retr 1
❖ -ERR S: <message 1 contents>
transaction phase, client: S: .
C: dele 1
list: list message numbers C: retr 2
retr: retrieve message by S: <message 1 contents>
number S: .
C: dele 2
dele: delete
C: quit
quit S: +OK POP3 server signing off
46
POP3 (more) and IMAP
More about POP3 IMAP
Previous example uses Keep all messages in
“download and delete” one place: the server
mode. Allows user to
Bob cannot re-read e- organize messages in
mail if he changes folders
client IMAP keeps user state
“Download-and-keep”: across sessions:
copies of messages on ❖ names of folders and
different clients mappings between
message IDs and folder
POP3 is stateless
name
across sessions
47
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
48
DNS: Domain Name System
49
DNS
DNS services Why not centralize DNS?
hostname to IP single point of failure
address translation traffic volume
host aliasing distant centralized
❖ Canonical, alias names database
mail server aliasing maintenance
load distribution
❖ replicated Web doesn’t scale!
servers: set of IP
addresses for one
canonical name
50
Distributed, Hierarchical Database
Root DNS Servers
13 root name
servers worldwide
b USC-ISI Marina del Rey, CA
l ICANN Los Angeles, CA
52
TLD and Authoritative Servers
Top-level domain (TLD) servers:
❖ responsible for com, org, net, edu, etc, and all
top-level country domains uk, fr, ca, jp.
❖ Network Solutions maintains servers for com TLD
❖ Educause for edu TLD
Authoritative DNS servers:
❖ organization’s DNS servers, providing
authoritative hostname to IP mappings for
organization’s servers (e.g., Web, mail).
❖ can be maintained by organization or service
provider
53
Local Name Server
does not strictly belong to hierarchy
each ISP (residential ISP, company,
university) has one.
❖ also called “default name server”
when host makes DNS query, query is sent
to its local DNS server
❖ acts as proxy, forwards query into hierarchy
54
DNS name root DNS server
resolution example
2
Host at cis.poly.edu 3
TLD DNS server
wants IP address for 4
gaia.cs.umass.edu 5
gaia.cs.umass.edu
55
DNS name
resolution example root DNS server
recursive query: 2 3
puts burden of name 6
7
resolution on
TLD DNS server
contacted name
server
heavy load? local DNS server
dns.poly.edu 5 4
1 8
gaia.cs.umass.edu
56
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
57
DNS records
DNS: distributed db storing resource records (RR)
RR format: (name, value, type, ttl)
Type=A Type=CNAME
❖ name is hostname ❖ name is alias name for some
❖ value is IP address “canonical” (the real) name
www.ibm.com is really
Type=NS
servereast.backup2.ibm.com
❖ name is domain (e.g.
❖ value is canonical name
foo.com)
❖ value is hostname of
Type=MX
authoritative name
❖ value is name of mailserver
server for this domain
associated with name
58
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
59
DNS protocol, messages
RRs in response
to query
records for
authoritative servers
additional “helpful”
info that may be used
60
Module 5: Application layer
1. Principles of 6. P2P applications
network applications 7. Socket programming
2. Web and HTTP with TCP
3. FTP 8. Socket programming
4. Electronic Mail with UDP
❖ SMTP, POP3, IMAP
5. DNS
61
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
62
Socket-programming using TCP
Socket: a door between application process and end-
transport protocol (UDP 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
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
63
Socket programming with TCP
Client must contact server When contacted by client,
server process must first server TCP creates new
be running socket for server process to
server must have created communicate with client
socket (door) that
welcomes client’s contact application viewpoint
TCP provides reliable, in-order
Client contacts server by:
transfer of bytes (“pipe”)
creating client-local TCP between client and server
socket
specifying IP address, port
number of server process
When client creates
socket: client TCP
establishes connection to
server TCP
64
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
65
Stream
keyboard monitor
A stream is a sequence of
characters that flow into
inFromUser
or out of a process. input
stream
inFromServer
outToServer
source, e.g., monitor or
output input
stream stream
socket.
client TCP
clientSocket
socket TCP
socket
66
Socket programming with TCP
Example client-server app:
1) client reads line from standard input (inFromUser stream) ,
sends to server via socket (outToServer stream)
2) server reads line from socket
3) server converts line to uppercase, sends back to client
4) client reads, prints modified line from socket
(inFromServer stream)
67
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');
clientSocket.close();
}
}
69
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
70
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();
71
Module-5: Application layer
2.1 Principles of 2.6 P2P applications
network applications 2.7 Socket programming
2.2 Web and HTTP with TCP
2.3 FTP 2.8 Socket programming
2.4 Electronic Mail with UDP
❖ SMTP, POP3, IMAP
2.5 DNS
72
Socket programming with UDP
73
Client/server socket interaction: UDP
Server (running on hostid) Client
write reply to
serverSocket
specifying read datagram from
client address, clientSocket
port number close
clientSocket
74
Example: Java client (UDP)
keyboard monitor
inFromUser
input
stream
Client
Process
Input: receives
process
packet (recall
Output: sends
thatTCP received
packet (recall “byte stream”)
receivePacket
sendPacket
that TCP sent UDP
packet
UDP
packet
“byte stream”)
client UDP
clientSocket
socket UDP
socket
75
Example: Java client (UDP)
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Translate
InetAddress IPAddress = InetAddress.getByName("hostname");
hostname to IP
address using DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
77
Example: Java server (UDP)
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
Create {
datagram socket
DatagramSocket serverSocket = new DatagramSocket(9876);
at port 9876
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
DatagramPacket receivePacket =
received datagram
new DatagramPacket(receiveData, receiveData.length);
Receive serverSocket.receive(receivePacket);
datagram
78
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
79
Module-5:Summary
our study of network apps now complete!
application architectures specific protocols:
❖ client-server ❖ HTTP
❖ P2P ❖ FTP