Ch_2 C_V7.01_J
Ch_2 C_V7.01_J
Chapter 2 ( 2 C )
Application Layer
Introduction 1-1
Chapter 2: outline
2.1 principles of network 2 C Slides:
applications 2.5 P2P applications ***
2.2 Web and HTTP 2.6 video streaming and
nt. traditional caching content distribution
(not in text) networks
2 B Slides: 2.7 socket programming
2.3 electronic mail with UDP and TCP
• SMTP, POP3, IMAP
2.4 DNS
increases linearly in N
Application Layer 2-5
File distribution time:
First Client Server Not to P2P
“Many hands make light work.”
F
us
di
network
ui
increases linearly in N …
… but so does this, as each peer brings service capacity
Application Layer 2-7
Comparison: Time to distribute to N
increases linearly in N
increases linearly in N …
… but so does this, as each peer brings service capacity
Application Layer 2-8
Client-server vs. P2P: example
“Many hands make light work.”
client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us
3.5
P2P
Minimum Distribution Time
3
Client-Server
2.5
1.5
0.5
0
0 5 10 15 20 25 30 35
N
Application Layer 2-9
P2P file distribution: BitTorrent
file divided into 256Kb chunks
peers in torrent send/receive file chunks
sharing
chunks
1 Alice arrives …
2 obtains list
of peers from tracker
3 begins exchanging p 144
file chunks with peers in torrent
Internet
p 149
Application Layer 2-20
CDN: Content distribution networks
challenge: stream content (selected from millions
of videos) to hundreds of thousands of
simultaneous users?
Solution: store/serve multiple copies of videos at
multiple geographically distributed sites (CDN)
• enter deep: push CDN servers deep into many access
networks
• close to users: In ISPs
• used by Akamai, 1700 locations
• bring home: smaller number (10’s) of larger clusters in
POPs near (but not within) access networks
• At IXPs (Intenet eXchange Points) (See Sec. 1.4)
• used by Limelight p 150
Application Layer 2-21
Content Distribution Networks (CDNs)
CDN: stores copies of content at CDN nodes
• e.g. Netflix stores copies of MadMen
subscriber requests content from CDN
• directed to nearby copy, retrieves content
• may choose different copy if network path congested
manifest file
where’s Madmen?
manifest file
where’s Madmen?
1. Bob manages
Netflix account CDN
server
4. DASH
streaming p 154
Application Layer 2-24
Chapter 2: outline
2.1 principles of network 2.5 P2P applications
applications 2.6 video streaming and
2.2 Web and HTTP content distribution
2.3 electronic mail networks
• SMTP, POP3, IMAP 2.7 socket programming
2.4 DNS with UDP and TCP **
application application
socket controlled by
process process app developer
transport transport
network network controlled
link by OS
link Internet
physical physical
p 157-8
Application Layer 2-26
Socket programming
Two socket types for two transport services:
• UDP: unreliable datagram
• TCP: reliable, byte stream-oriented
Application Example:
1. client reads a line of characters (data) from its
keyboard and sends data to server
2. server receives the data and converts characters
to uppercase
3. server sends modified data to client
4. client receives modified data & displays on screen
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket p 160
Application 2-29
Example app: UDP client
Python UDPClient
include Python’s socket
library
from socket import *
serverName = ‘hostname’
serverPort = 12000
create UDP socket for clientSocket = socket(AF_INET,
server
SOCK_DGRAM)
get user keyboard
input message = raw_input(’Input lowercase sentence:’)
Attach server name, port to clientSocket.sendto(message.encode(),
message; send into socket
(serverName, serverPort))
read reply characters from modifiedMessage, serverAddress =
socket into string
clientSocket.recvfrom(2048)
print out received string print modifiedMessage.decode()
and close socket
p 161-2
clientSocket.close()
Application Layer 2-30
Example app: UDP server
Python UDPServer
from socket import *
serverPort = 12000
create UDP socket serverSocket = socket(AF_INET, SOCK_DGRAM)
bind socket to local port
number 12000
serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
loop forever while True:
Read from UDP socket into message, clientAddress = serverSocket.recvfrom(2048)
message, getting client’s
address (client IP and port) modifiedMessage = message.decode().upper()
send upper case string serverSocket.sendto(modifiedMessage.encode(),
back to this client
clientAddress)
p 163-4
Application Layer 2-31
Socket programming with TCP
client must contact server when contacted by client,
server process must first be server TCP creates new
running socket for server process
server must have created to communicate with that
welcome socket (door) particular client
for client(s) to contact • allows server to talk with
multiple clients
client contacts server by: • source port numbers used
Creating TCP socket, to distinguish clients
specifying IP address, port (more in Chap 3)
number of server process
when client creates socket: application viewpoint:
client TCP establishes TCP provides reliable, in-order
connection to server TCP byte-stream transfer (“pipe”)
between client and server
p 165
Application Layer 2-32
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
p 167
Application Layer 2-33
Example app: TCP client
Python TCPClient
from socket import *
serverName = ’servername’
create TCP socket for
serverPort = 12000
server, remote port 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
No need to attach server clientSocket.send(sentence.encode())
name, port
modifiedSentence = clientSocket.recv(1024)
print (‘From Server:’, modifiedSentence.decode())
clientSocket.close()
p 166-8
Application Layer 2-34
Example app: TCP server
Python TCPServer
from socket import *
create TCP welcoming serverPort = 12000
socket serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
server begins listening for
incoming TCP requests serverSocket.listen(1)
print ‘The server is ready to receive’
loop forever
while True:
server waits on accept()
for incoming requests, new
connectionSocket, addr = serverSocket.accept()
socket created on return
sentence = connectionSocket.recv(1024).decode()
read bytes from socket (but
not address as in UDP) capitalizedSentence = sentence.upper()
close connection to this connectionSocket.send(capitalizedSentence.
client (but not welcoming
socket) encode())
connectionSocket.close()
Application Layer 2-35
Chapter 2: summary
our study of network apps now complete!
application architectures specific protocols:
• client-server • HTTP
• P2P • SMTP, POP, IMAP
application service
requirements: • DNS
• reliability, bandwidth, delay • P2P: BitTorrent
Internet transport service video streaming, CDNs
model socket programming:
• connection-oriented,
TCP, UDP sockets
reliable: TCP
• unreliable, datagrams: UDP