3. Lecture 3: 9-4-01 3
Applications and Application-Layer
Protocols
โข Application: communicating,
distributed processes
โข Running in network hosts in
โuser spaceโ
โข Exchange messages to
implement app
โข e.g., email, file transfer, the
Web
โข Application-layer protocols
โข One โpieceโ of an app
โข Define messages exchanged
by apps and actions taken
โข User services provided by
lower layer protocols
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
4. Lecture 3: 9-4-01 4
Client-Server Paradigm
Typical network app has two pieces: client and server
application
transport
network
data link
physical
application
transport
network
data link
physical
Client:
โข Initiates contact with server
(โspeaks firstโ)
โข Typically requests service from
server,
โข For Web, client is implemented in
browser; for e-mail, in mail reader
Server:
โข Provides requested service to
client
โข e.g., Web server sends
requested Web page, mail server
delivers e-mail
request
reply
5. Lecture 3: 9-4-01 5
Ftp: The File Transfer Protocol
โข Transfer file to/from remote host
โข Client/server model
โข Client: side that initiates transfer (either to/from remote)
โข Server: remote host
โข ftp: RFC 959
โข ftp server: port 21
file transfer
FTP
server
FTP
user
interface
FTP
client
local file
system
remote file
system
user
at host
6. Lecture 3: 9-4-01 6
Ftp: Separate Control, Data
Connections
โข Ftp client contacts ftp server
at port 21, specifying TCP
as transport protocol
โข Two parallel TCP
connections opened:
โข Control: exchange commands,
responses between client,
server.
โout of band controlโ
โข Data: file data to/from server
โข Ftp server maintains โstateโ:
current directory, earlier
authentication
FTP
client
FTP
server
TCP control connection
port 21
TCP data connection
port 20
7. Lecture 3: 9-4-01 7
Ftp Commands, Responses
Sample Commands:
โข sent as ASCII text over
control channel
โข USER username
โข PASS password
โข LIST return list of files in
current directory
โข RETR filename
retrieves (gets) file
โข STOR filename stores
(puts) file onto remote host
Sample Return Codes
โข status code and phrase
โข 331 Username OK,
password required
โข 125 data connection
already open;
transfer starting
โข 425 Canโt open data
connection
โข 452 Error writing
file
8. Lecture 3: 9-4-01 8
What Transport Service Does an
Application Need?
Data loss
โข Some apps (e.g., audio) can
tolerate some loss
โข Other apps (e.g., file transfer,
telnet) require 100% reliable
data transfer
Timing
โข Some apps (e.g., Internet
telephony, interactive
games) require low delay to
be โeffectiveโ
Bandwidth
โข Some apps (e.g., multimedia) require minimum amount of
bandwidth to be โeffectiveโ
โข Other apps (โelastic appsโ) make use of whatever bandwidth they
get
9. Lecture 3: 9-4-01 9
Transport Service Requirements
of Common Apps
no loss
no loss
no loss
loss-tolerant
loss-tolerant
loss-tolerant
no loss
elastic
elastic
elastic
audio: 5Kb-1Mb
video:10Kb-5Mb
same as above
few Kbps
elastic
no
no
no
yes, 100โs msec
yes, few secs
yes, 100โs msec
yes and no
file transfer
e-mail
web documents
real-time audio/
video
stored audio/video
interactive games
financial apps
Application Data loss Bandwidth Time Sensitive
11. Lecture 3: 9-4-01 11
Server and Client
TCP/UDP
IP
Ethernet Adapter
Server
TCP/UDP
IP
Ethernet Adapter
Clients
Server and Client exchange messages over the
network through a common Socket API
Socket API
hardware
kernel
space
user
space
ports
12. Lecture 3: 9-4-01 12
User Datagram Protocol(UDP):
An Analogy
Postal Mail
โข Single mailbox to receive
messages
โข Unreliable ๏
โข Not necessarily in-order
delivery
โข Each letter is independent
โข Must address each reply
Example UDP applications
Multimedia, voice over IP
UDP
โข Single socket to receive
messages
โข No guarantee of delivery
โข Not necessarily in-order
delivery
โข Datagram โ independent
packets
โข Must address each packet
Postal Mail
โข Single mailbox to receive
letters
โข Unreliable ๏
โข Not necessarily in-order
delivery
โข Letters sent independently
โข Must address each reply
13. Lecture 3: 9-4-01 13
Transmission Control Protocol
(TCP): An Analogy
TCP
โข Reliable โ guarantee
delivery
โข Byte stream โ in-order
delivery
โข Connection-oriented โ
single socket per
connection
โข Setup connection
followed by data transfer
Telephone Call
โข Guaranteed delivery
โข In-order delivery
โข Connection-oriented
โข Setup connection
followed by conversation
Example TCP applications
Web, Email, Telnet
14. Lecture 3: 9-4-01 14
Network Addressing Analogy
412-268-8000
ext.123
Central Number
Applications/Servers
Web
Port 80
Mail
Port 25
Exchange
Area Code
412-268-8000
ext.654
IP Address
Network No.
Host Number
Telephone No
15-441 Students Clients
Professors at CMU
Network Programming
Telephone Call
Port No.
Extension
15. Lecture 3: 9-4-01 15
Concept of Port Numbers
โข Port numbers are used to identify
โentitiesโ on a host
โข Port numbers can be
โข Well-known (port 0-1023)
โข Dynamic or private (port 1024-65535)
โข Servers/daemons usually use well-
known ports
โข Any client can identify the server/service
โข HTTP = 80, FTP = 21, Telnet = 23, ...
โข /etc/service defines well-known ports
โข Clients usually use dynamic ports
โข Assigned by the kernel at run time
TCP/UDP
IP
Ethernet Adapter
NTP
daemon
Web
server
port 123 port 80
16. Lecture 3: 9-4-01 16
Names and Addresses
โข Each attachment point on Internet is given
unique address
โข Based on location within network โ like phone
numbers
โข Humans prefer to deal with names not
addresses
โข DNS provides mapping of name to address
โข Name based on administrative ownership of
host
17. Lecture 3: 9-4-01 17
#include <netinet/in.h>
/* Internet address structure */
struct in_addr {
u_long s_addr; /* 32-bit IPv4 address */
}; /* network byte ordered */
/* Socket address, Internet style. */
struct sockaddr_in {
u_char sin_family; /* Address Family */
u_short sin_port; /* UDP or TCP Port# */
/* network byte ordered */
struct in_addr sin_addr; /* Internet Address */
char sin_zero[8]; /* unused */
};
Internet Addressing Data Structure
โข sin_family = AF_INET selects Internet address family
19. Lecture 3: 9-4-01 19
Byte Ordering Functions
โข Converts between host byte order and network
byte order
โข โhโ = host byte order
โข โnโ = network byte order
โข โlโ = long (4 bytes), converts IP addresses
โข โsโ = short (2 bytes), converts port numbers
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);
unsigned short int htons(unsigned short int
hostshort);
unsigned long int ntohl(unsigned long int netlong);
unsigned short int ntohs(unsigned short int
netshort);
21. Lecture 3: 9-4-01 21
โข A socket is a file descriptor that lets an application read/write data
from/to the network
โข socket returns an integer (socket descriptor)
โข fd < 0 indicates that an error occurred
โข socket descriptors are similar to file descriptors
โข AF_INET: associates a socket with the Internet protocol family
โข SOCK_STREAM: selects the TCP protocol
โข SOCK_DGRAM: selects the UDP protocol
What is a Socket?
int fd; /* socket descriptor */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(โsocketโ);
exit(1);
}
22. Lecture 3: 9-4-01 22
TCP
IP
Ethernet Adapter
Web Server
Port 80
โข For example: web
server
โข What does a web server
need to do so that a web
client can connect to it?
TCP Server
23. Lecture 3: 9-4-01 23
โข Since web traffic uses TCP, the web server must create a socket of type
SOCK_STREAM
int fd; /* socket descriptor */
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(โsocketโ);
exit(1);
}
โข socket returns an integer (socket descriptor)
โข fd < 0 indicates that an error occurred
โข AF_INET associates a socket with the Internet protocol family
โข SOCK_STREAM selects the TCP protocol
Socket I/O: socket()
24. Lecture 3: 9-4-01 24
โข A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* create the socket */
srv.sin_family = AF_INET; /* use the Internet addr family */
srv.sin_port = htons(80); /* bind socket โfdโ to port 80*/
/* bind: a client may connect to any of my addresses */
srv.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror("bind"); exit(1);
}
โข Still not quite ready to communicate with a client...
Socket I/O: bind()
25. Lecture 3: 9-4-01 25
Socket I/O: listen()
โข listen indicates that the server will accept a connection
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* 1) create the socket */
/* 2) bind the socket to a port */
if(listen(fd, 5) < 0) {
perror(โlistenโ);
exit(1);
}
โข Still not quite ready to communicate with a client...
26. Lecture 3: 9-4-01 26
Socket I/O: accept()
โข accept blocks waiting for a connection
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
/* 1) create the socket */
/* 2) bind the socket to a port */
/* 3) listen on the socket */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept"); exit(1);
}
โข accept returns a new socket (newfd) with the same properties as the
original socket (fd)
โข newfd < 0 indicates that an error occurred
27. Lecture 3: 9-4-01 27
Socket I/O: accept() continued...
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept");
exit(1);
}
โข How does the server know which client it is?
โข cli.sin_addr.s_addr contains the clientโs IP address
โข cli.sin_port contains the clientโs port number
โข Now the server can exchange data with the client by
using read and write on the descriptor newfd.
โข Why does accept need to return a new descriptor?
28. Lecture 3: 9-4-01 28
Socket I/O: read()
โข read can be used with a socket
โข read blocks waiting for data from the client but
does not guarantee that sizeof(buf) is read
int fd; /* socket descriptor */
char buf[512]; /* used by read() */
int nbytes; /* used by read() */
/* 1) create the socket */
/* 2) bind the socket to a port */
/* 3) listen on the socket */
/* 4) accept the incoming connection */
if((nbytes = read(newfd, buf, sizeof(buf))) < 0) {
perror(โreadโ); exit(1);
}
29. Lecture 3: 9-4-01 29
TCP
IP
Ethernet Adapter
2 Web Clients
TCP Client
โข For example: web
client
โข How does a web client
connect to a web server?
30. Lecture 3: 9-4-01 30
struct sockaddr_in srv;
srv.sin_addr.s_addr = inet_addr(โ128.2.35.50โ);
if(srv.sin_addr.s_addr == (in_addr_t) -1) {
fprintf(stderr, "inet_addr failed!n"); exit(1);
}
Converting a numerical address to a string:
Dealing with IP Addresses
โข IP Addresses are commonly written as strings (โ128.2.35.50โ), but
programs deal with IP addresses as integers.
struct sockaddr_in srv;
char *t = inet_ntoa(srv.sin_addr);
if(t == 0) {
fprintf(stderr, โinet_ntoa failed!nโ); exit(1);
}
Converting strings to numerical address:
31. Lecture 3: 9-4-01 31
Translating Names to Addresses
โข Gethostbyname provides interface to DNS
โข Additional useful calls
โข Gethostbyaddr โ returns hostent given sockaddr_in
โข Getservbyname
โข Used to get service description (typically port number)
โข Returns servent based on name
#include <netdb.h>
struct hostent *hp; /*ptr to host info for remote*/
struct sockaddr_in peeraddr;
char *name = โwww.cs.cmu.eduโ;
peeraddr.sin_family = AF_INET;
hp = gethostbyname(name)
peeraddr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
32. Lecture 3: 9-4-01 32
Socket I/O: connect()
โข connect allows a client to connect to a server...
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
/* create the socket */
/* connect: use the Internet address family */
srv.sin_family = AF_INET;
/* connect: socket โfdโ to port 80 */
srv.sin_port = htons(80);
/* connect: connect to IP Address โ128.2.35.50โ */
srv.sin_addr.s_addr = inet_addr(โ128.2.35.50โ);
if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror(โconnect"); exit(1);
}
33. Lecture 3: 9-4-01 33
Socket I/O: write()
โข write can be used with a socket
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
char buf[512]; /* used by write() */
int nbytes; /* used by write() */
/* 1) create the socket */
/* 2) connect() to the server */
/* Example: A client could โwriteโ a request to a server
*/
if((nbytes = write(fd, buf, sizeof(buf))) < 0) {
perror(โwriteโ);
exit(1);
}
35. Lecture 3: 9-4-01 35
UDP
IP
Ethernet Adapter
NTP
daemon
UDP Server Example
Port 123
โข For example: NTP
daemon
โข What does a UDP server
need to do so that a UDP
client can connect to it?
36. Lecture 3: 9-4-01 36
Socket I/O: socket()
โข The UDP server must create a datagram socketโฆ
int fd; /* socket descriptor */
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror(โsocketโ);
exit(1);
}
โข socket returns an integer (socket descriptor)
โข fd < 0 indicates that an error occurred
โข AF_INET: associates a socket with the Internet protocol family
โข SOCK_DGRAM: selects the UDP protocol
37. Lecture 3: 9-4-01 37
Socket I/O: bind()
โข A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* create the socket */
/* bind: use the Internet address family */
srv.sin_family = AF_INET;
/* bind: socket โfdโ to port 80*/
srv.sin_port = htons(80);
/* bind: a client may connect to any of my addresses */
srv.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror("bind"); exit(1);
}
โข Now the UDP server is ready to accept packetsโฆ
38. Lecture 3: 9-4-01 38
Socket I/O: recvfrom()
โข read does not provide the clientโs address to the UDP server
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in cli; /* used by recvfrom() */
char buf[512]; /* used by recvfrom() */
int cli_len = sizeof(cli); /* used by recvfrom() */
int nbytes; /* used by recvfrom() */
/* 1) create the socket */
/* 2) bind to the socket */
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) &cli, &cli_len);
if(nbytes < 0) {
perror(โrecvfromโ); exit(1);
}
39. Lecture 3: 9-4-01 39
Socket I/O: recvfrom() continued...
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) cli, &cli_len);
โข The actions performed by recvfrom
โข returns the number of bytes read (nbytes)
โข copies nbytes of data into buf
โข returns the address of the client (cli)
โข returns the length of cli (cli_len)
โข donโt worry about flags
40. Lecture 3: 9-4-01 40
TCP
IP
Ethernet Adapter
2 UDP Clients
UDP Client Example
ports
โข How does a UDP client
communicate with a UDP
server?
41. Lecture 3: 9-4-01 41
Socket I/O: sendto()
โข write is not allowed
โข Notice that the UDP client does not bind a port number
โข a port number is dynamically assigned when the first sendto is called
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by sendto() */
/* 1) create the socket */
/* sendto: send data to IP Address โ128.2.35.50โ port 80 */
srv.sin_family = AF_INET;
srv.sin_port = htons(80);
srv.sin_addr.s_addr = inet_addr(โ128.2.35.50โ);
nbytes = sendto(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) &srv, sizeof(srv));
if(nbytes < 0) {
perror(โsendtoโ); exit(1);
}
42. Lecture 3: 9-4-01 42
Review: UDP Client-Server
Interaction
socket()
bind()
recvfrom()
sendto()
UDP Server
socket()
UDP Client
sendto()
recvfrom()
close()
blocks until datagram
received from a client
data request
data reply
from UNIX Network Programming Volume 1, figure 8.1
43. Lecture 3: 9-4-01 43
UDP
IP
Ethernet Adapter
UDP Server
The UDP Server
Port 2000
Port 3000
โข How can the UDP server
service multiple ports
simultaneously?
44. Lecture 3: 9-4-01 44
int s1; /* socket descriptor 1 */
int s2; /* socket descriptor 2 */
/* 1) create socket s1 */
/* 2) create socket s2 */
/* 3) bind s1 to port 2000 */
/* 4) bind s2 to port 3000 */
while(1) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
recvfrom(s2, buf, sizeof(buf), ...);
/* process buf */
}
UDP Server: Servicing Two Ports
โข What problems does this code have?
45. Lecture 3: 9-4-01 45
Socket I/O: select()
int select(int maxfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
FD_CLR(int fd, fd_set *fds); /* clear the bit for fd in fds */
FD_ISSET(int fd, fd_set *fds); /* is the bit for fd in fds? */
FD_SET(int fd, fd_set *fds); /* turn on the bit for fd in fds */
FD_ZERO(fd_set *fds); /* clear all bits in fds */
โข maxfds: number of descriptors to be tested
โข descriptors (0, 1, ... maxfds-1) will be tested
โข readfds: a set of fds we want to check if data is available
โข returns a set of fds ready to read
โข if input argument is NULL, not interested in that condition
โข writefds: returns a set of fds ready to write
โข exceptfds: returns a set of fds with exception conditions
46. Lecture 3: 9-4-01 46
Socket I/O: select()
int select(int maxfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
struct timeval {
long tv_sec; /* seconds /
long tv_usec; /* microseconds */
}
โข timeout
โข if NULL, wait forever and return only when one of the descriptors is
ready for I/O
โข otherwise, wait up to a fixed amount of time specified by timeout
โข if we donโt want to wait at all, create a timeout structure with timer value
equal to 0
โข Refer to the man page for more information
47. Lecture 3: 9-4-01 47
int s1, s2; /* socket descriptors */
fd_set readfds; /* used by select() */
/* create and bind s1 and s2 */
while(1) {
FD_ZERO(&readfds); /* initialize the fd set
*/
FD_SET(s1, &readfds); /* add s1 to the fd set */
FD_SET(s2, &readfds); /* add s2 to the fd set */
if(select(s2+1, &readfds, 0, 0, 0) < 0) {
perror(โselectโ);
exit(1);
}
if(FD_ISSET(s1, &readfds)) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
}
/* do the same for s2 */
}
Socket I/O: select()
โข select allows synchronous I/O multiplexing
48. Lecture 3: 9-4-01 48
TCP
IP
Ethernet Adapter
Web Server
Port 80
How can a a web server manage
multiple connections simultaneously?
Port 8001
More Details About a Web Server
49. Lecture 3: 9-4-01 49
int fd, next=0; /* original socket */
int newfd[10]; /* new socket descriptors */
while(1) {
fd_set readfds;
FD_ZERO(&readfds); FD_SET(fd, &readfds);
/* Now use FD_SET to initialize other newfdโs
that have already been returned by accept() */
select(maxfd+1, &readfds, 0, 0, 0);
if(FD_ISSET(fd, &readfds)) {
newfd[next++] = accept(fd, ...);
}
/* do the following for each descriptor newfd[n] */
if(FD_ISSET(newfd[n], &readfds)) {
read(newfd[n], buf, sizeof(buf));
/* process data */
}
}
Socket I/O: select()
โข Now the web server can support multiple connections...