Internetworking II: Network programmingApril 20, 2000 Topics• client/server model• Berkeley sockets–TCP client and server examples–UDP client and server examples• I/O multiplexing with select()15-213class26.pptCS 213 S’00–2–class26.pptInternet protocol stack Reliable byte streamdelivery(process-process)Unreliablebest effort datagramdelivery(host-host)Unreliablebest effortdatagramdelivery(process-process)User application program (FTP, Telnet, WWW, email)User datagram protocol(UDP)Transmission control protocol (TCP)Internet Protocol (IP)Network interface (ethernet)hardwarePhysical connectionBerkeley sockets interfaceCS 213 S’00–3–class26.pptUDP vs TCPUser Datagram Protocol (UDP)• unreliable datagrams from process to process • thin veneer over IP• similar to sending surface mail– each message is an independent chunk of data (datagram)– messages may not arrive or may arrive out of order• faster than TCP, requires no server state, but ureliableTransmission Control Protocol (TCP)• reliable byte-stream from process to process)• complex implementation• similar to placing a phone call– no messages, just a continuous stream of bytes over a connection– bytes arrive in order • slower and requires more resources, but cleaner user semanticsCS 213 S’00–4–class26.pptBerkeley Sockets InterfaceCreated in the early 80’s as part of the original Berkeley distribution of Unix that contained the TCP/IP protocol stack.Provides user-level interface to UDP and TCPUnderlying basis for all Internet applications.Based on client/server programming modelCS 213 S’00–5–class26.pptClient/server programming modelClient + server = distributed computingClient & 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’00–6–class26.pptInternet ServersServers are long-running processes (daemons).• Created at boot-time (typically) by the init process• Run continuously until the machine is turned off.Each server waits for either TCP connection requests or UDP datagrams 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’00–7–class26.pptServer examplesWeb server (port 80)• resource: files/compute cycles (CGI programs)• service: retrieves files and runs CGI programs on behalf of the clientFTP server (20, 21)• resource: files• service: stores and retrieve 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 fileCS 213 S’00–8–class26.pptServer examples (cont)DNS name server (53)• resource: distributed name database • service: distributed database lookupWhois server (430)• resource: second level domain name database (e.g. cmu.edu)• service: database lookupDaytime (13)• resource: system clock• service: retrieves value of system clockDHCP server (67)• resource: IP addresses• service: assigns IP addresses to clientsCS 213 S’00–9–class26.pptServer examples (cont)X server (177)• resource: display screen and keyboard• service: paints screen and accepts keyboard input on behalf of aclientAFS file server (7000)• resource: subset of files in a distributed filesystem (e.g., AFS, NFS)• service: retrieves and stores filesKerberos authentication server (750)• resource: “tickets”• service: authenticates client and returns tickets/etc/services file gives a comprehensive list for Linux machines.CS 213 S’00–10–class26.pptFile 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);}CS 213 S’00–11–class26.pptFile 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’00–12–class26.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’00–13–class26.pptWhat is a socket?A socketis a descriptor that lets an application read/write from/to the network.• Unix uses the same abstraction for both file I/O and network I/O.Clients and servers communicate with each other via TCP and UDP using the same socket abstraction.• applications read and write TCP byte streams by reading from andwriting to socket descriptors.• applications read write UDP datagrams by reading from and writing to socket descriptors.Main difference between file I/O and socket I/O is how the application “opens” the sock descriptors.CS 213 S’00–14–class26.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’00–15–class26.pptKey data structures Defined in /usr/include/netdb.hHostent is a DNS host entry that associates a domain name (e.g.,
View Full Document