Chapter 2
Application Layer
A note on the use of these ppt slides: Computer
We’re making these slides freely available to all (faculty, students, readers).
They’re in PowerPoint form so you see the animations; and can add, modify, Networking: A
and delete slides (including this one) and slide content to suit your needs.
They obviously represent a lot of work on our part. In return for use, we only Top Down
ask the following:
If you use these slides (e.g., in a class) that you mention their source Approach
(after all, we’d like people to use our book!)
If you post any slides on a www site, that you note that they are adapted
6th edition
from (or perhaps identical to) our slides, and note our copyright of this Jim Kurose, Keith Ross
material. Addison-Wesley
Thanks and enjoy! JFK/KWR March 2012
All material copyright 1996-2012
J.F Kurose and K.W. Ross, All Rights Reserved
Application Layer 2-1
Chapter 2: outline
2.1 principles of network 2.6 P2P applications
applications 2.7 socket programming
app architectures with UDP and TCP
app requirements
2.2 Web and HTTP
2.3 FTP
2.4 electronic mail
SMTP, POP3, IMAP
2.5 DNS
Application Layer 2-2
Socket programming
goal: learn how to build client/server applications that
communicate using sockets
socket: door between application process and end-end-
transport protocol
application application
socket controlled by
process process app developer
transport transport
network network controlled
link
by OS
link Internet
physical physical
Application Layer 2-3
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 the data to the server.
2. The server receives the data and converts
characters to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays
the line on its screen.
Application Layer 2-4
Socket programming with UDP
UDP: no “ connection” between client & server
no handshaking before sending data
sender explicitly attaches IP destination address and
port # to each packet
rcvr extracts sender IP address and port# from
received packet
UDP: transmitted data may be lost or received
out-of-order
Application viewpoint:
UDP provides unreliable transfer of groups of bytes (
“ datagrams” ) between client and server
Application Layer 2-5
Client/server socket interaction: UDP
server (running on serverIP) client
create socket:
create socket, port= x: clientSocket =
serverSocket = socket(AF_INET,SOCK_DGRAM)
socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
read datagram from clientSocket
serverSocket
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Application 2-6
Example app: UDP client
Python UDPClient
include Python’s socket
library from socket import *
serverName = ‘hostname’
serverPort = 12000
create UDP socket for
server
clientSocket = socket(socket.AF_INET,
get user keyboard socket.SOCK_DGRAM)
input
message = raw_input(’Input lowercase sentence:’)
Attach server name, port to
message; send into socket clientSocket.sendto(message,(serverName, serverPort))
read reply characters from modifiedMessage, serverAddress =
socket into string
clientSocket.recvfrom(2048)
print out received string print modifiedMessage
and close socket
clientSocket.close()
Application Layer 2-7
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 1:
Read from UDP socket into
message, getting client’s
message, clientAddress = serverSocket.recvfrom(2048)
address (client IP and port) modifiedMessage = message.upper()
send upper case string serverSocket.sendto(modifiedMessage, clientAddress)
back to this client
Application Layer 2-8
Socket programming with TCP
client must contact server when contacted by client,
server process must first be server TCP creates new socket
running for server process to
communicate with that
server must have created particular client
socket (door) that welcomes
client’s contact allows server to talk with
multiple clients
client contacts server by: source port numbers used to
Creating TCP socket, distinguish clients (more in
specifying IP address, port 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
Application Layer 2-9
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()
wait for incoming create socket,
connection request
TCP connect to hostid, port=x
connectionSocket = connection setup clientSocket = socket()
serverSocket.accept()
send request using
read request from clientSocket
connectionSocket
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Application Layer 2-10
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
name, port clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
Application Layer 2-11
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 1:
server waits on accept()
for incoming requests, new
connectionSocket, addr = serverSocket.accept()
socket created on return
read bytes from socket (but
sentence = connectionSocket.recv(1024)
not address as in UDP) capitalizedSentence = sentence.upper()
close connection to this connectionSocket.send(capitalizedSentence)
client (but not welcoming
socket) connectionSocket.close()
Application Layer 2-12
Chapter 2: summary
our study of network apps now complete!
application architectures specific protocols:
client-server HTTP
P2P
FTP
application service
requirements: SMTP, POP, IMAP
reliability, bandwidth, delay DNS
Internet transport service model P2P: BitTorrent, DHT
connection-oriented, socket programming: TCP,
reliable: TCP
UDP sockets
unreliable, datagrams: UDP
Application Layer 2-13
Chapter 2: summary
most importantly: learned about protocols!
typical request/reply important themes:
message exchange:
client requests info or
control vs. data msgs
service in-band, out-of-band
server responds with centralized vs. decentralized
data, status code
stateless vs. stateful
message formats:
headers: fields giving reliable vs. unreliable msg
info about data transfer
data: info being “ complexity at network edge”
communicated
Application Layer 2-14
Chapter 1
Additional Slides
Introduction 1-15
application
packet (www browser,
analyzer email client)
application
OS
packet Transport (TCP/UDP)
copy of all Network (IP)
capture Ethernet
frames Link (Ethernet)
(pcap) sent/receive
d Physical