Professional Documents
Culture Documents
Chapter2 Application Layer Update
Chapter2 Application Layer Update
Application Layer
clients:
communicate with server
client/server may be intermittently
connected
may have dynamic IP
addresses
do not communicate directly
with each other
Application Layer 2-7
P2P architecture
no always-on server peer-peer
arbitrary end systems
directly communicate
peers request service from
other peers, provide service
in return to other peers
self scalability – new
peers bring new service
capacity, as well as new
service demands
peers are intermittently
connected and change IP
addresses
complex management
application application
socket controlled by
process process app developer
transport transport
network network controlled
link
by OS
link Internet
physical physical
application underlying
application layer protocol transport protocol
end-point
authentication
Application Layer 2-17
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
time
6. Steps 1-5 repeated for each of 10
jpeg objects
ebay 8734
usual http request msg Amazon server
cookie file creates ID
usual http response
1678 for user create backend
ebay 8734
set-cookie: 1678 entry database
amazon 1678
usual http request msg
cookie: 1678 cookie- access
specific
usual http response msg action
… …
m. WIDE Tokyo
e. NASA Mt View, CA (5 other sites)
f. Internet Software C.
Palo Alto, CA (and 48 other sites)
gaia.cs.umass.edu
examples:
file distribution
(BitTorrent)
Streaming (KanKan)
VoIP (Skype)
Socket API
Introduced in BSD4.1 UNIX, 1981
Explicitly created, used, released by
socket
applications
A host-local, application-
Client/server paradigm
Two types of transport service via socket
created, OS-controlled
API: interface (a “door”) into which
Reliable, byte stream-oriented (TCP) application process can both
Unreliable datagram (UDP) send and receive messages
to/from another application
process
API: Application Programming Interface
BSD: Berkeley Standard Distribution (Unix)
Applica
tion
Socket-programming using TCP
controlled by
controlled by application
application process process
developer
developer socket
socket controlled by
controlled by
TCP TCP
operating operating
with internet with system
system
buffers, buffers,
variable variable
host or
host or s
s
server server
Applica
tion
Socket programming with TCP
Client must contact server When contacted by client, server
Server process must first TCP creates new socket for server
be running process to communicate with client
Server must have created Allows server to talk with
socket (door) that multiple clients
welcomes client’s contact Source port numbers used to
Client contacts server by: distinguish clients
Creating client-local TCP
socket
Specifying IP address, port
number of server process
When client creates socket:
client TCP establishes Application viewpoint
connection to server TCP
TCP provides reliable, in-order
transfer of bytes (“pipe”) between
client and server
Applica
tion
Socket programming with TCP
keyboar monito
Client
stream) process
Applica
tion
Client/server socket interaction: TCP
Server (running on Client
hostid)
create socket,
port=x, for incoming
request:
welcomeSocket =
ServerSocket()
write reply to
connectionSocket
read reply from
clientSocket
close
connectionSocket close
clientSocket
Applica
tion
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Applica
tion
Example: Java server (TCP), cont
Create
output stream, DataOutputStream outToClient =
attached new DataOutputStream(connectionSocket.getOutputStream());
to socket
Read in
line from clientSentence =
socket inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase()
Write out + '\n';
outToClient.writeBytes(capitalizedSente
line
} nce);
to socket }
} End of while loop, loop
back and wait for
another client
connection
Applica
tion
Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient
{
clientSocket.close();
}
}
Applica
tion
Chapter 2: Application layer
2.7 Socket programming
2.1 Principles of network with TCP
applications 2.8 Socket programming
2.2 Web and HTTP with UDP
2.3 FTP
2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
Applica
tion
Socket programming with UDP
Applica
tion
Client/server socket interaction: UDP
Server (running on Client
hostid)
create socket, create socket,
port=x, for incoming clientSocket =
request: DatagramSocket()
serverSocket =
DatagramSocket()
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number
close
clientSocket
Applica
tion
Example: Java server (UDP)
import java.io.*; import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
{
Cre
ate datagram DatagramSocket serverSocket = new DatagramSocket(9876);
socket at port
9876 byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space
DatagramPacket receivePacket =
for received new DatagramPacket(receiveData, receiveData.length);
datagram serverSocket.receive(receivePacket);
Receive
datagram
Applica
tion
Example: Java server (UDP),
cont
String sentence = new String(receivePacket.getData());
Get IP addr port
#, of sender InetAddress IPAddress = receivePacket.getAddress();
sendData = capitalizedSentence.getBytes();
Create
DatagramPacket sendPacket =
datagram to new DatagramPacket(sendData, sendData.length, IPAddress, port);
send to client
Write
serverSocket.send(sendPacket);
out }
datagra }
m to }
socket End of while loop,
loop back and wait
for another
datagram
Applica
tion
Example: Java client (UDP)
keyboard monitor
input
stream
Client
Input:
process
receives
Output: sends
packet (UDP
packet (UDP received
sent “byte UDP UDP “byte
packet packe
stream”) t stream”)
clientSocket
client
UDPsocke UDP
socket
t from network
to network
Applica
tion
Example: Java client (UDP)
import java.io.*; import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
Crea
BufferedReader inFromUser =
te input new BufferedReader(new InputStreamReader(System.in));
stream
DatagramSocket clientSocket = new DatagramSocket();
Crea
te clientTransl InetAddress IPAddress = InetAddress.getByName(“localhost");
atesocket
hostname to byte[] sendData = new byte[1024];
IP address using byte[] receiveData = new byte[1024];
DNS
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Applica
tion
Example: Java client (UDP),
cont.
Create datagram
with DatagramPacket sendPacket =
data-to-send, new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
length, IP addr,
Send clientSocket.send(sendPacket);
port datagram
DatagramPacket receivePacket =
to server new DatagramPacket(receiveData, receiveData.length);
Read
clientSocket.receive(receivePacket);
datagram
from String modifiedSentence =
server new String(receivePacket.getData());
Applica
tion