3rd Edition: Chapter 2

Download Report

Transcript 3rd Edition: Chapter 2

Chapter 2
Application Layer
Computer Networking:
A Top Down Approach,
5th edition.
Jim Kurose, Keith Ross
Addison-Wesley, April
2009.
2: Application Layer
1
Chapter 2: Application layer
 2.1 Principles of
network applications
 2.2 Web and HTTP
 2.3 FTP
 2.4 Electronic Mail

 2.6 P2P applications
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
SMTP, POP3, IMAP
 2.5 DNS
2: Application Layer
2
recv and send
 recv may be used instead of read (and is recommended):
n = recv(fd, buffer, count, flags);
 The first three arguments are the same as for read. The
flags argument provides additional flexibility. Interesting
flags are MSG_DONTWAIT and MSG_PEEK. Refer to
recv(2) for details.
2: Application Layer
3
recv and send
 Similarly send may be used instead of write (and is also
recommended):
n = send(fd, buffer, count, flags);
 Interesting flags are MSG_DONTWAIT, MSG_MORE and
MSG_DONTROUTE. Refer to send(2) for details.
2: Application Layer
4
Chapter 2: Application layer
 2.1 Principles of
network applications
 2.2 Web and HTTP
 2.3 FTP
 2.4 Electronic Mail

 2.6 P2P applications
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
SMTP, POP3, IMAP
 2.5 DNS
2: Application Layer
5
Socket programming with UDP
UDP: no “connection” between
client and server
 no handshaking
 sender explicitly attaches
IP address and port of
destination to each packet
 server must extract IP
address, port of sender
from received packet
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
UDP: transmitted data may be
received out of order, or
lost
2: Application Layer
6
Client/server socket interaction: UDP
Server (running on hostid)
create socket,
port= x.
srvfd = socket()
bind()
read datagram using
recvfrom
write reply using
sendto
specifying
client address,
port number
Client
create socket,
clifd = socket()
Create datagram with server IP and
port=x; send datagram via
sendto
read datagram using
recvfrom
close
close()
2: Application Layer
7
UDP Programming
 The client functions for passing UDP datagrams are:
socket(), sendto(), and recvfrom(). It is not necessary for
the client to connect() with the server. (Although connect()
can be used.)
 The server uses socket(), bind(), recvfrom(), and sendto().
The server does not accept() a connection from the client,
instead recvfrom() returns the data and client address.
2: Application Layer
8
UDP Programming
 The sendto() routine takes a destination address structure
as an argument:
typedef struct sockaddr SA;
struct sockaddr_in srvaddr_in;
memset(&srvaddr_in, 0, sizeof(srvaddr_in));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(9000);
inet_pton(AF_INET, “10.10.0.9”,
&srvaddr_in.sin_addr.s_addr);
sfd = socket(AF_INET, SOCK_DGRAM, 0);
sendto(sfd, msg, msglen, 0, (SA *)&srvaddr_in,
sizeof(srvaddr_in));
2: Application Layer
9
UDP Programming
 recvfrom() returns an address as an argument. The address
is used by the server to respond to the proper client:
n = recvfrom(sockfd, mesg, mesg_len, 0,
(SA *)&cliaddr_in, &len);
sendto(sockfd, mesg, n, 0, (SA *)&cliaddr_in, len);
 A NULL address may be used in recvfrom() if we do not
care about the address.
 Refer to udp_server.cpp for a complete example of UDP
socket programming (a modified echo server).
 In-class Exercise: Write a udp_client.cpp program to
communicate with udp_server.cpp. It should prompt the
user for input, send the input to the server, and then read
and display the response.
2: Application Layer
10
Chapter 2: Summary
our study of network apps now complete!
 application architectures
 client-server
 P2P
 hybrid
 application service
requirements:

reliability, bandwidth,
delay
 specific protocols:
 HTTP
 FTP
 SMTP, POP, IMAP
 DNS
 P2P: BitTorrent, Skype
 socket programming
 Internet transport
service model


connection-oriented,
reliable: TCP
unreliable, datagrams: UDP
2: Application Layer
11
Chapter 2: Summary
Most importantly: learned about protocols
 typical request/reply
message exchange:


client requests info or
service
server responds with
data, status code
 message formats:
 headers: fields giving
info about data
 data: info being
communicated
Important themes:
 control vs. data msgs
 in-band, out-of-band
 centralized vs.
decentralized
 stateless vs. stateful
 reliable vs. unreliable
msg transfer
 “complexity at network
edge”
2: Application Layer
12