Page 1Network programmingApril 19, 2001Topics• Client-server model• Sockets interface• Echo client and serverclass24.ppt15-213“The course that gives CMU its Zip!”CS 213 S’01– 2 –class24.pptClient-server programming modelClient & server are both processesServer manages a resourceClient makes a request for a service• request may involve a conversation according to some server protocolServer provides service by manipulating the resource on behalf of client and then returning a responseclient serverrequestclient serverresponseclient serverprocessrequestCS 213 S’01– 3 –class24.pptClientsExamples of client programs• Web browsers, ftp, telnet, sshHow does the client find the server?• The address of the server process has two parts: IPaddress:port–The IP address is a unique 32-bit positive integer that identifies the machine.» dotted decimal form: 0x8002C2F2 = 128.2.194.242–The port is positive integer associated with a service (and thus a server) on that machine. » port 7: echo server» port 23: telnet server» port 25: mail server» port 80: web serverCS 213 S’01– 4 –class24.pptUsing ports to identify servicesclientWeb server(port 80)client machineserver machine 128.2 194.242kernelEcho server(port 7)service request for128.2.194.242:80(i.e., the Web server)clientWeb server(port 80)kernelEcho server(port 7)service request for128.2.194.242:7(i.e., the echo server)Page 2CS 213 S’01– 5 –class24.pptServersServers are long-running processes (daemons).• Created at boot-time (typically) by the init process (process 1)• Run continuously until the machine is turned off.Each server waits for requests to arrive on a well-known port associated with a particular service.• port 7: echo server• port 25: mail server• port 80: http serverA machine that runs a server process is also often referred to as a “server”.CS 213 S’01– 6 –class24.pptServer examplesWeb server (port 80)• resource: files/compute cycles (CGI programs)• service: serves files and runs CGI programs on behalf of the clientFTP server (20, 21)• resource: files• service: stores and serves filesTelnet server (23)• resource: terminal• service: proxies a terminal on the server machineMail server (25)• resource: email “spool” file• service: stores mail messages in spool file See /etc/services for a comprehensive listof the services available on a Linux machine.CS 213 S’01– 7 –class24.pptThe two basic ways that clients and servers communicateConnections:• reliable two-way byte-stream.• looks like a file.• akin to placing a phone call.• slower but more robust.Datagrams: • data transferred in unreliable chunks.• can be lost or arrive out of order.• akin to using surface mail.• faster but less robust.We will only discuss connections.client server... , Bk, Bk-1, ... , B1, B0B0, B1, ..., Bk-1, Bk, ...connectionclient serverdgram dgramdgramdgramCS 213 S’01– 8 –class24.pptLinux file I/O: open()Must open() a file before you can do anything else.open() returns a small integer (file descriptor)fd < 0 indicates that an error occurredpredefined file descriptors:• 0: stdin• 1: stdout• 2: stderrint fd; /* file descriptor */if ((fd = open(“/etc/hosts”, O_RDONLY)) < 0) {perror(“open”);exit(1);}Page 3CS 213 S’01– 9 –class24.pptLinux file I/O: read()read() allows a program to access the contents of file.read() returns the number of bytes read from file fd.nbytes < 0 indicates that an error occurred.• if successful, read() places nbytes bytes into memory starting at address bufchar buf[512];int fd; /* file descriptor */int nbytes; /* number of bytes read *//* open the file *//* read up to 512 bytes from file fd */if ((nbytes = read(fd, buf, sizeof(buf))) < 0) {perror(“read”);exit(1);}CS 213 S’01– 10 –class24.pptFile I/O: write()write() allows a program to modify file contents.write() returns the number of bytes written from bufto file fd.nbytes < 0 indicates that an error occurred.char buf[512];int fd; /* file descriptor */int nbytes; /* number of bytes read *//* open the file *//* write up to 512 bytes from buf to file fd */if ((nbytes = write(fd, buf, sizeof(buf)) < 0) {perror(“write”);exit(1);}CS 213 S’01– 11 –class24.pptBerkeley Sockets InterfaceCreated in the early 80’s as part of the original Berkeley distribution of Unix that contained an early version of the Internet protocols.Provides a user-level interface to the network.Underlying basis for all Internet applications.Based on client/server programming model.CS 213 S’01– 12 –class24.pptWhat is a socket?A socket is a descriptor that lets an application read/write from/to the network.• Key idea: Linux uses the same abstraction for both file I/O and network I/O.Clients and servers communicate with each other by reading from and writing to socket descriptors.• Using regular Linux read and write I/O functions.The main difference between file I/O and socket I/O is how the application “opens” the socket descriptors.Page 4CS 213 S’01– 13 –class24.pptKey data structuresDefined in /usr/include/netinet/in.hInternet-style sockets are characterized by a 32-bit IP address and a port./* Internet address */struct in_addr {unsigned int s_addr; /* 32-bit IP address */ };/* Internet style socket address */struct sockaddr_in {unsigned short int sin_family; /* Address family (AF_INET) */unsigned short int sin_port; /* Port number */struct in_addr sin_addr; /* IP address */unsigned char sin_zero[...]; /* Pad to sizeof “struct sockaddr” */};CS 213 S’01– 14 –class24.pptKey data structures Defined in /usr/include/netdb.hHostent is a DNS host entry that associates a domain name (e.g., cmu.edu) with an IP addr (128.2.35.186)• DNS (Domain Name Service) is a world-wide distributed database of domain name/IP address mappings.• Can be accessed from user programs using gethostbyname()[domain name to IP address] or gethostbyaddr() [IP address to domain name]• Can also be accessed from the shell using nslookup or dig./* Domain Name Service (DNS) host entry */struct hostent {char *h_name; /* official name of host */char **h_aliases; /* alias list */int h_addrtype; /* host address type */int h_length; /* length of address */char **h_addr_list; /* list of addresses */}CS 213 S’01– 15 –class24.pptEcho client: prologue/* * error - wrapper for perror*/void error(char *msg)
View Full Document