Computer Network
Computer Network
Computer Network
• Application layer
• Client-server
15-441: Computer Networking • Application requirements
• Background
Lecture 3: Application Layer and • TCP vs. UDP
Socket Programming • Byte ordering
• Socket I/O
• TCP/UDP server and client
• I/O multiplexing
Lecture 3: 9 -4-01 2
•1
What Transport Service Does an
Ftp Commands, Responses Application Need?
Sample Commands: Sample Return Codes Data loss Timing
• sent as ASCII text over • Some apps (e.g., audio) can • Some apps (e.g., Internet
• status code and phrase
control channel tolerate some loss telephony, interactive
• 331 Username OK, • Other apps (e.g., file transfer, games) require low delay to
• USER username password required be “effective”
telnet) require 100% reliable
• PASS password data transfer
• 125 data connection
• LIST return list of files in already open;
current directory transfer starting Bandwidth
• RETR filename • 425 Can’t open data • Some apps (e.g., multimedia) require minimum amount of
retrieves (gets) file bandwidth to be “effective”
connection
• Other apps (“elastic apps”) make use of whatever bandwidth they
• STOR filename stores • 452 Error writing
(puts) file onto remote host get
file
IP IP
Example UDP applications
Ethernet Adapter Ethernet Adapter hardware Multimedia, voice over IP
Lecture 3: 9 -4-01 11 Lecture 3: 9 -4-01 12
•2
Transmission Control Protocol Network Addressing Analogy
(TCP): An Analogy
TCP Telephone Call Telephone Call Network Programming
• Reliable – guarantee • Guaranteed delivery Professors at CMU Applications/Servers
delivery • In-order delivery 412-268-8000 412-268-8000
ext.123 ext.654 Web Mail
• Byte stream – in-order • Connection-oriented Port 80 Port 25
delivery Extension Port No.
• Setup connection
• Connection-oriented – followed by conversation
single socket per Telephone No IP Address
connection Central Number Network No.
• Setup connection Exchange Host Number
followed by data transfer
Area Code
Example TCP applications
Web, Email, Telnet 15-441 Students Clients
•3
Byte Ordering Functions Lecture Overview
• AF_INET: associates a socket with the Internet protocol family Ethernet Adapter
• SOCK_STREAM: selects the TCP protocol
• SOCK_DGRAM: selects the UDP protocol
•4
Socket I/O: listen() Socket I/O: accept()
• listen indicates that the server will accept a connection • accept blocks waiting for a connection
int fd; /* socket descriptor */
int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in srv; /* used by bind() */ struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
/* 1) create the socket */ int cli_len = sizeof(cli); /* used by accept() */
/* 2) bind the socket to a port */
/* 1) create the socket */
/* 2) bind the socket to a port */
if(listen(fd, 5) < 0) {
/* 3) listen on the socket */
perror(“listen”);
exit(1); newfd = accept (fd, (struct sockaddr*) &cli, &cli_len);
} if(newfd < 0) {
perror("accept"); exit(1);
• Still not quite ready to communicate with a client... }
• accept returns a new socket ( newfd) with the same properties as the
original socket ( fd)
• newfd < 0 indicates that an error occurred
Lecture 3: 9 -4-01 25 Lecture 3: 9 -4-01 26
• How does the server know which client it is? /* 1) create the socket */
• cli.sin_addr.s_addr contains the client’s IP address /* 2) bind the socket to a port */
/* 3) listen on the socket */
• cli.sin_port contains the client’s port number /* 4) accept the incoming connection */
• Now the server can exchange data with the client by if((nbytes = read(newfd, buf, sizeof(buf))) < 0) {
using read and write on the descriptor newfd. perror(“read”); exit(1);
}
• Why does accept need to return a new descriptor?
Lecture 3: 9 -4-01 27 Lecture 3: 9 -4-01 28
•5
Translating Names to Addresses Socket I/O: connect()
close()
Lecture 3: 9 -4-01 33 from UNIX Network Programming Volume 1, figure 4.1 Lecture 3: 9 -4-01 34
•6
Socket I/O: bind() Socket I/O: recvfrom()
• A socket can be bound to a port • read does not provide the client’s address to the UDP server
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */ int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* create the socket */
struct sockaddr_in cli; /* used by recvfrom() */
/* bind: use the Internet address family */ char buf [512]; /* used by recvfrom () */
srv.sin_family = AF_INET; int cli_ len = sizeof(cli ); /* used by recvfrom () */
int nbytes; /* used by recvfrom() */
/* bind: socket ‘fd’ to port 80*/
srv.sin_port = htons(80); /* 1) create the socket */
/* 2) bind to the socket */
/* bind: a client may connect to any of my addresses */
srv.sin_addr.s_addr = htonl(INADDR_ANY);
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { (struct sockaddr*) &cli, &cli_len);
perror("bind"); exit(1); if(nbytes < 0) {
} perror(“recvfrom”); exit(1);
• Now the UDP server is ready to accept packets… }
Lecture 3: 9 -4-01 41 from UNIX Network Programming Volume 1, figure 8.1 Lecture 3: 9 -4-01 42
•7
The UDP Server UDP Server: Servicing Two Ports
int s1; /* socket descriptor 1 */
int s2; /* socket descriptor 2 */
while(1) {
UDP recvfrom (s1, buf, sizeof(buf), ...);
/* process buf */
•8
A Few Programming Notes:
Socket I/O: select() Representing Packets
int fd, next=0; /* original socket */
int newfd[10]; /* new socket descriptors */ 0 1 2 3
while(1) { 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
fd_set readfds; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
FD_ZERO(&readfds); FD_SET(fd, &readfds); | Type |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/* Now use FD_SET to initialize other newfd’s | Length | Checksum |
that have already been returned by accept() */ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Address |
select (maxfd+1, &readfds, 0, 0, 0); +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
if(FD_ISSET(fd, &readfds)) {
newfd[ next++] = accept (fd, ...); Type: 4-byte integer
} Length: 2-byte integer
/* do the following for each descriptor newfd[n] */ Checksum: 2-byte integer
if(FD_ISSET(newfd[ n], &readfds)) { Address: 4 -byte IP address
read(newfd[n], buf, sizeof(buf));
/* process data */
}
}
• Now the web server can support multiple connections...
Lecture 3: 9 -4-01 49 Lecture 3: 9 -4-01 50
•9