Computer Network

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lecture Overview

• 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

Applications and Application-Layer Client-Server Paradigm


Protocols
• Application: communicating, Typical network app has two pieces: client and server
distributed processes application
transport Client:
network
• Running in network hosts in data link • Initiates contact with server application
transport
physical
“user space” (“speaks first”) network
data link
physical
• Exchange messages to • Typically requests service from
implement app server, request
• e.g., email, file transfer, the • For Web, client is implemented in
Web browser; for e-mail, in mail
reader
• Application-layer protocols
• One “piece” of an app application Server: reply
application transport
transport
• Define messages exchanged network
network
data link
• Provides requested service to application
data link transport
by apps and actions taken physical
physical client network
data link
• User services provided by • e.g., Web server sends physical

lower layer protocols requested Web page, mail server


delivers e-mail
Lecture 3: 9 -4-01 3 Lecture 3: 9 -4-01 4

Ftp: Separate Control, Data


Ftp: The File Transfer Protocol Connections
• Ftp client contacts ftp server
at port 21, specifying TCP
FTP file transfer
user
FTP FTP as transport protocol
client server
interface • Two parallel TCP
user TCP control connection
local file remote file connections opened: port 21
at host
system system • Control: exchange commands,
responses between client,
• Transfer file to/from remote host server. TCP data connection
FTP port 20 FTP
“out of band control” client server
• Client/server model
• Data: file data to/from server
• Client: side that initiates transfer (either to/from
• Ftp server maintains “state”:
remote)
current directory, earlier
• Server: remote host authentication
• ftp: RFC 959
• ftp server: port 21
Lecture 3: 9 -4-01 5 Lecture 3: 9 -4-01 6

•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

Lecture 3: 9 -4-01 7 Lecture 3: 9 -4-01 8

Transport Service Requirements Lecture Overview


of Common Apps
Application Data loss Bandwidth Time Sensitive • Application layer
• Client-server
file transfer no loss elastic no
e-mail no loss elastic no • Application requirements
web documents no loss elastic no • Background
real-time audio/ loss-tolerant audio: 5Kb-1Mb yes, 100’s msec
video video:10Kb-5Mb • TCP vs. UDP
stored audio/video loss-tolerant same as above yes, few secs
yes, 100’s msec
• Byte ordering
interactive games loss-tolerant few Kbps
financial apps no loss elastic yes and no • Socket I/O
• TCP/UDP server and client
• I/O multiplexing

Lecture 3: 9 -4-01 9 Lecture 3: 9 -4-01 10

User Datagram Protocol(UDP):


Server and Client An Analogy
Server and Client exchange messages over the UDP Postal Mail
network through a common Socket API • Single socket to receive • Single mailbox to receive
messages messages
letters
Clients
Server user • No guarantee of delivery • Unreliable J
ports space • Not necessarily in-order • Not necessarily in-order
delivery delivery
• Datagram – independent • Letters
Each letter
sentisindependently
independent
TCP/UDP TCP/UDP packets • Must address each reply
Socket API kernel
• Must address each packet
space

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

Lecture 3: 9 -4-01 13 Lecture 3: 9 -4-01 14

Concept of Port Numbers Names and Addresses


• Port numbers are used to identify • Each attachment point on Internet is given
“entities” on a host NTP
daemon
Web
server
unique address
• Port numbers can be
• Well-known (port 0-1023) port 123
• Based on location within network – like phone
port 80
• Dynamic or private (port 1024-65535) numbers
• Servers/daemons usually use well- TCP/UDP • Humans prefer to deal with names not
known ports addresses
• Any client can identify the server/service
IP • DNS provides mapping of name to address
• HTTP = 80, FTP = 21, Telnet = 23, ...
• Whitepages of the Internet
• /etc/service defines well-known ports
• Clients usually use dynamic ports Ethernet Adapter • Name based on administrative ownership of
• Assigned by the kernel at run time host

Lecture 3: 9 -4-01 15 Lecture 3: 9 -4-01 16

Internet Addressing Data Structure Byte Ordering

#include <netinet/in.h> union {


u_int32_t addr; /* 4 bytes address */
/* Internet address structure */ char c[4];
struct in_addr { } un;
u_long s_addr; /* 32-bit IPv4 address */ /* 128.2.194.95 */
}; /* network byte ordered */ un.addr = 0x8002c25f;
/* c[0] = ? */
/* Socket address, Internet style. */ c[0] c[1] c[2] c[3]
struct sockaddr_in {
u_char sin_family; /* Address Family */
• Big Endian 128 2 194 95
u_short sin_port; /* UDP or TCP Port# */
/* network byte ordered */ • Sun Solaris, PowerPC, ...
struct in_addr sin_addr; /* Internet Address */ • Little Endian 95 194 2 128
char sin_zero[8]; /* unused */
}; • i386, alpha, ...
• Network byte order = Big Endian
• sin_family = AF_INET selects Internet address family
Lecture 3: 9 -4-01 17 Lecture 3: 9 -4-01 18

•3
Byte Ordering Functions Lecture Overview

• Converts between host byte order and network • Application layer


byte order • Client-server
• ‘h’ = host byte order
• Application requirements
• ‘n’ = network byte order
• ‘l’ = long (4 bytes), converts IP addresses • Background
• ‘s’ = short (2 bytes), converts port numbers • TCP vs. UDP
#include <netinet/in.h> • Byte ordering
unsigned long int htonl(unsigned long int hostlong); • Socket I/O
unsigned short int htons(unsigned short int
hostshort); • TCP/UDP server and client
unsigned long int ntohl(unsigned long int netlong); • I/O multiplexing
unsigned short int ntohs(unsigned short int
netshort );
Lecture 3: 9 -4-01 19 Lecture 3: 9 -4-01 20

What is a Socket? TCP Server


• A socket is a file descriptor that lets an application read/write data
from/to the network • For example: web
int fd; /* socket descriptor */ Web Server
server
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(“socket”); Port 80
exit(1); • What does a web server need to
}
TCP do so that a web client can
• socket returns an integer (socket descriptor)
connect to it?
• fd < 0 indicates that an error occurred
• socket descriptors are similar to file descriptors IP

• AF_INET: associates a socket with the Internet protocol family Ethernet Adapter
• SOCK_STREAM: selects the TCP protocol
• SOCK_DGRAM: selects the UDP protocol

Lecture 3: 9 -4-01 21 Lecture 3: 9 -4-01 22

Socket I/O: socket() Socket I/O: bind()


• Since web traffic uses TCP, the web server must create a socket • A socket can be bound to a port
of type SOCK_STREAM
int fd; /* socket descriptor */
int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */

/* create the socket */


if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(“socket”); srv.sin_family = AF_INET; /* use the Internet addr family */
exit(1);
srv.sin_port = htons(80); /* bind socket ‘fd’ to port 80*/
}
/* bind: a client may connect to any of my addresses */
• socket returns an integer ( socket descriptor) srv.sin_addr.s_addr = htonl(INADDR_ANY);
• fd < 0 indicates that an error occurred
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
• AF_INET associates a socket with the Internet protocol family perror("bind"); exit(1);
}
• SOCK_STREAM selects the TCP protocol
• Still not quite ready to communicate with a client...
Lecture 3: 9 -4-01 23 Lecture 3: 9 -4-01 24

•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

Socket I/O: accept() continued... Socket I/O: read()


struct sockaddr_in cli; /* used by accept() */ • read can be used with a socket
int newfd; /* returned by accept() */
int cli_ len = sizeof(cli ); /* used by accept() */ • read blocks waiting for data from the client but
does not guarantee that sizeof ( buf) is read
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept"); int fd; /* socket descriptor */
exit(1); char buf[512]; /* used by read() */
} int nbytes; /* used by read() */

• 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

TCP Client Dealing with IP Addresses


• IP Addresses are commonly written as strings (“128.2.35.50”), but
• For example: web programs deal with IP addresses as integers.
client 2 Web Clients
Converting strings to numerical address:
struct sockaddr_in srv;
• How does a web client connect srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
to a web server ? if(srv.sin_addr.s_addr == (in_addr_t) -1) {
TCP fprintf(stderr, "inet_addr failed!\ n"); exit(1);
}

Converting a numerical address to a string:


IP
struct sockaddr_in srv;
char *t = inet_ntoa(srv.sin_addr);
Ethernet Adapter if(t == 0) {
fprintf(stderr, “inet_ntoa failed!\ n”); exit(1);
}
Lecture 3: 9 -4-01 29 Lecture 3: 9 -4-01 30

•5
Translating Names to Addresses Socket I/O: connect()

• Gethostbyname provides interface to DNS • connect allows a client to connect to a server...


int fd; /* socket descriptor */
• Additional useful calls struct sockaddr_in srv; /* used by connect() */
• Gethostbyaddr – returns hostent given sockaddr_in
/* create the socket */
• Getservbyname
• Used to get service description (typically port number) /* connect: use the Internet address family */
srv.sin_family = AF_INET;
• Returns servent based on name
#include < netdb.h> /* connect: socket ‘fd’ to port 80 */
srv.sin_port = htons(80);
struct hostent *hp; /* ptr to host info for remote*/
struct sockaddr_in peeraddr; /* connect: connect to IP Address “128.2.35.50” */
char *name = “www. cs.cmu.edu”; srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);

peeraddr.sin_family = AF_INET; if(connect (fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {


hp = gethostbyname (name) perror(”connect"); exit(1);
peeraddr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr; }

Lecture 3: 9 -4-01 31 Lecture 3: 9 -4-01 32

Review: TCP Client-Server


Socket I/O: write() Interaction
TCP Server
• write can be used with a socket
socket()
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */ bind()
char buf[512]; /* used by write() */ listen()
int nbytes; /* used by write() */ TCP Client
socket() accept()
/* 1) create the socket */ connection establishment
/* 2) connect() to the server */ connect()

data request read()


/* Example: A client could “write” a request to a server write()
*/
if((nbytes = write(fd, buf, sizeof(buf))) < 0) { data reply write()
perror(“write”); read()
exit(1);
} close() end-of-file notification read()

close()
Lecture 3: 9 -4-01 33 from UNIX Network Programming Volume 1, figure 4.1 Lecture 3: 9 -4-01 34

UDP Server Example Socket I/O: socket()

• The UDP server must create a datagram socket…


• For example: NTP
NTP
daemon int fd; /* socket descriptor */
daemon
if((fd = socket(AF_INET, SOCK_DGRAM , 0)) < 0) {
Port 123 perror(“socket”);
• What does a UDP server need to
exit(1);
do so that a UDP client can }
UDP connect to it?
• socket returns an integer ( socket descriptor)
• fd < 0 indicates that an error occurred
IP

• AF_INET: associates a socket with the Internet protocol family


Ethernet Adapter
• SOCK_DGRAM: selects the UDP protocol

Lecture 3: 9 -4-01 35 Lecture 3: 9 -4-01 36

•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 37 Lecture 3: 9 -4-01 38

Socket I/O: recvfrom() continued... UDP Client Example


nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) cli, &cli_len);
2 UDP Clients

• The actions performed by recvfrom • How does a UDP client


• returns the number of bytes read (nbytes) communicate with a UDP
server ?
• copies nbytes of data into buf ports

• returns the address of the client (cli) TCP

• returns the length of cli (cli_len)


IP
• don’t worry about flags
Ethernet Adapter

Lecture 3: 9 -4-01 39 Lecture 3: 9 -4-01 40

Review: UDP Client-Server


Socket I/O: sendto() Interaction
• write is not allowed
UDP Server
• Notice that the UDP client does not bind a port number
• a port number is dynamically assigned when the first sendto is called socket()

int fd; /* socket descriptor */ bind()


struct sockaddr_in srv; /* used by sendto() */
UDP Client
recvfrom ()
/* 1) create the socket */ socket()
blocks until datagram
/* sendto: send data to IP Address “128.2.35.50” port 80 */ received from a client
sendto() data request
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)); data reply sendto()
if(nbytes < 0) { recvfrom ()
perror(“sendto”); exit(1);
} close()

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 */

• How can the UDP server /* 1) create socket s1 */


UDP Server service multiple ports /* 2) create socket s2 */
Port 3000 Port 2000 simultaneously? /* 3) bind s1 to port 2000 */
/* 4) bind s2 to port 3000 */

while(1) {
UDP recvfrom (s1, buf, sizeof(buf), ...);
/* process buf */

IP recvfrom (s2, buf, sizeof(buf), ...);


/* process buf */
}
Ethernet Adapter
• What problems does this code have?

Lecture 3: 9 -4-01 43 Lecture 3: 9 -4-01 44

Socket I/O: select() Socket I/O: select()


int select(int maxfds, f d_set *readfds, f d_set *writefds, int select(int maxfds, f d_set *readfds, f d_set *writefds,
fd_set * exceptfds, struct timeval *timeout); fd_set * exceptfds, struct timeval *timeout );
FD_CLR(int fd, fd_set *fds ); /* clear the bit for fd in fds */
struct timeval {
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 */ long tv_sec; /* seconds /
FD_ZERO(fd_set *fds); /* clear all bits in fds */ long tv_ usec; /* microseconds */
}
• maxfds: number of descriptors to be tested
• timeout
• descriptors (0, 1, ... maxfds-1) will be tested • if NULL, wait forever and return only when one of the
• readfds: a set of fds we want to check if data is available descriptors is ready for I/O
• returns a set of fds ready to read • otherwise, wait up to a fixed amount of time specified by
timeout
• if input argument is NULL, not interested in that condition
• if we don’t want to wait at all, create a timeout structure with timer
• writefds: returns a set of fds ready to write value equal to 0

• exceptfds: returns a set of fds with exception conditions


• Refer to the man page for more information
Lecture 3: 9 -4-01 45 Lecture 3: 9 -4-01 46

Socket I/O: select() More Details About a Web Server


• select allows synchronous I/O multiplexing
int s1, s2; /* socket descriptors */
How can a a web server manage
fd_set readfds; /* used by select() */
multiple connections simultaneously?
/* create and bind s1 and s2 */
Web Server
while(1) {
FD_ZERO(&readfds); /* initialize the fd set Port 8001 Port 80
*/
FD_SET(s1, &readfds); /* add s1 to the fd set */
FD_SET(s2, &readfds); /* add s2 to the fd set */
TCP
if(select (s2+1, &readfds, 0, 0, 0) < 0) {
perror(“select”);
exit(1);
} IP
if(FD_ISSET(s1, &readfds)) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
} Ethernet Adapter
/* do the same for s2 */
}

Lecture 3: 9 -4-01 47 Lecture 3: 9 -4-01 48

•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

A Few Programming Notes: Socket Programming References


Building a Packet in a Buffer
struct packet { • Man page
u_int32_t type;
u_int16_t length; • usage: man <function name>
u_int16_t checksum;
u_int32_t address; • Textbook
};
• Sections 2.6, 2.7
/* ================================================== */ • demo programs written in Java
char buf[1024];
struct packet *pkt; • Unix Network Programming : Networking
pkt = (struct packet*) buf; APIs: Sockets and XTI (Volume 1)
pkt->type = htonl(1);
pkt->length = htons (2);
• Section 2, 3, 4, 6, 8
pkt->checksum = htons(3); • ultimate socket programming bible!
pkt->address = htonl(4);

Lecture 3: 9 -4-01 51 Lecture 3: 9 -4-01 52

•9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy