Lecture 03
Lecture 03
• Application layer
• Client-server
• Application requirements
• Background
• TCP vs. UDP
• Byte ordering
• Socket I/O
• TCP/UDP server and client
• I/O multiplexing
Lecture 3: 9-4-01 2
Applications and Application-Layer
Protocols
• Application: communicating,
distributed processes application
transport
network
• Running in network hosts in data link
physical
“user space”
• Exchange messages to
implement app
• e.g., email, file transfer, the
Web
• Application-layer protocols
• One “piece” of an app application
application
transport
transport network
• Define messages exchanged network data link
data link physical
by apps and actions taken physical
delivers e-mail
Lecture 3: 9-4-01 4
Ftp: The File Transfer Protocol
Lecture 3: 9-4-01 5
Ftp: Separate Control, Data
Connections
• Ftp client contacts ftp server
at port 21, specifying TCP
as transport protocol
• Two parallel TCP TCP control connection
connections opened: port 21
• Control: exchange commands,
responses between client,
server. TCP data connection
FTP port 20 FTP
“out of band control” client server
• Data: file data to/from server
• Ftp server maintains “state”:
current directory, earlier
authentication
Lecture 3: 9-4-01 6
Ftp Commands, Responses
Lecture 3: 9-4-01 7
What Transport Service Does an
Application Need?
Data loss Timing
• Some apps (e.g., audio) can • Some apps (e.g., Internet
tolerate some loss telephony, interactive
• Other apps (e.g., file transfer, games) require low delay to
telnet) require 100% reliable be “effective”
data transfer
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
Lecture 3: 9-4-01 8
Transport Service Requirements
of Common Apps
Application Data loss Bandwidth Time Sensitive
Lecture 3: 9-4-01 9
Lecture Overview
• Application layer
• Client-server
• Application requirements
• Background
• TCP vs. UDP
• Byte ordering
• Socket I/O
• TCP/UDP server and client
• I/O multiplexing
Lecture 3: 9-4-01 10
Server and Client
Server and Client exchange messages over the
network through a common Socket API
Clients
user
Server
ports space
TCP/UDP TCP/UDP
Socket API kernel
space
IP IP
Lecture 3: 9-4-01 11
User Datagram Protocol(UDP):
An Analogy
UDP Postal Mail
• Single socket to receive • Single mailbox to receive
messages messages
letters
• No guarantee of delivery • Unreliable
• Not necessarily in-order • Not necessarily in-order
delivery delivery
• Datagram – independent • Letters
Each letter
sentisindependently
independent
packets • Must address each reply
• Must address each packet • Must address each reply
Telephone No IP Address
Central Number Network No.
Exchange Host Number
Area Code
Lecture 3: 9-4-01 14
Concept of Port Numbers
• Port numbers are used to identify
“entities” on a host NTP Web
• Port numbers can be daemon server
• Well-known (port 0-1023) port 123 port 80
• Dynamic or private (port 1024-65535)
• Servers/daemons usually use well- TCP/UDP
known ports
• Any client can identify the server/service
IP
• HTTP = 80, FTP = 21, Telnet = 23, ...
• /etc/service defines well-known ports
Ethernet Adapter
• Clients usually use dynamic ports
• Assigned by the kernel at run time
Lecture 3: 9-4-01 15
Names and Addresses
Lecture 3: 9-4-01 16
Internet Addressing Data Structure
#include <netinet/in.h>
Lecture 3: 9-4-01 18
Byte Ordering Functions
• Application layer
• Client-server
• Application requirements
• Background
• TCP vs. UDP
• Byte ordering
• Socket I/O
• TCP/UDP server and client
• I/O multiplexing
Lecture 3: 9-4-01 20
What is a Socket?
• A socket is a file descriptor that lets an application read/write data
from/to the network
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
• socket descriptors are similar to file descriptors
Lecture 3: 9-4-01 21
TCP Server
Ethernet Adapter
Lecture 3: 9-4-01 22
Socket I/O: socket()
• Since web traffic uses TCP, the web server must create a socket of type
SOCK_STREAM
Lecture 3: 9-4-01 23
Socket I/O: bind()
• A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
if(listen(fd, 5) < 0) {
perror(“listen”);
exit(1);
}
Lecture 3: 9-4-01 25
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() */
Lecture 3: 9-4-01 26
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() */
Lecture 3: 9-4-01 28
TCP Client
IP
Ethernet Adapter
Lecture 3: 9-4-01 29
Dealing with IP Addresses
• IP Addresses are commonly written as strings (“128.2.35.50”), but
programs deal with IP addresses as integers.
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:
peeraddr.sin_family = AF_INET;
hp = gethostbyname(name)
peeraddr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
Lecture 3: 9-4-01 31
Socket I/O: connect()
• connect allows a client to connect to a server...
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
Lecture 3: 9-4-01 32
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() */
Lecture 3: 9-4-01 33
Review: TCP Client-Server
Interaction
TCP Server
socket()
bind()
socket() accept()
connection establishment
connect()
data request read()
write()
close()
from UNIX Network Programming Volume 1, figure 4.1 Lecture 3: 9-4-01 34
UDP Server Example
IP
Ethernet Adapter
Lecture 3: 9-4-01 35
Socket I/O: socket()
• The UDP server must create a datagram socket…
Lecture 3: 9-4-01 36
Socket I/O: bind()
• A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
Lecture 3: 9-4-01 38
Socket I/O: recvfrom() continued...
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) cli, &cli_len);
Lecture 3: 9-4-01 39
UDP Client Example
2 UDP Clients
IP
Ethernet Adapter
Lecture 3: 9-4-01 40
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
Lecture 3: 9-4-01 41
Review: UDP Client-Server
Interaction
UDP Server
socket()
bind()
UDP Client
recvfrom()
socket()
blocks until datagram
sendto() received from a client
data request
close()
UDP
IP
Ethernet Adapter
Lecture 3: 9-4-01 43
UDP Server: Servicing Two Ports
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 */
Lecture 3: 9-4-01 44
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
Lecture 3: 9-4-01 46
Socket I/O: select()
• select allows synchronous I/O multiplexing
int s1, s2; /* socket descriptors */
fd_set readfds; /* used by select() */
Lecture 3: 9-4-01 47
More Details About a Web Server
TCP
IP
Ethernet Adapter
Lecture 3: 9-4-01 48
Socket I/O: select()
int fd, next=0; /* original socket */
int newfd[10]; /* new socket descriptors */
while(1) {
fd_set readfds;
FD_ZERO(&readfds); FD_SET(fd, &readfds);
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Lecture 3: 9-4-01 50
A Few Programming Notes:
Building a Packet in a Buffer
struct packet {
u_int32_t type;
u_int16_t length;
u_int16_t checksum;
u_int32_t address;
};
/* ================================================== */
char buf[1024];
struct packet *pkt;
Lecture 3: 9-4-01 51
Socket Programming References
• Man page
• usage: man <function name>
• Textbook
• Sections 2.6, 2.7
• demo programs written in Java
• Unix Network Programming : Networking
APIs: Sockets and XTI (Volume 1)
• Section 2, 3, 4, 6, 8
• ultimate socket programming bible!
Lecture 3: 9-4-01 52