Internetworking II: Network programming April 20, 2000Internet protocol stackUDP vs TCPBerkeley Sockets InterfaceClient/server programming modelInternet ServersServer examplesServer examples (cont)Slide 9File I/O: open()File I/O: read()File I/O: write()What is a socket?Key data structuresKey data structuresTCP echo server: prologueTCP echo server: socket()TCP echo server: setsockopt()TCP echo server: init socket addressTCP echo server: bind()TCP echo server: listen()TCP echo server: main loopTCP echo server: accept()TCP echo server: identifying clientTCP echo server: read()TCP echo server: write()Testing the TCP server with telnetTCP client: prologueTCP client: socket()TCP client: gethostbyname()TCP client: connect()TCP client: read(), write(), close()Running the TCP client and serverUDP echo server: socket(), bind()UDP echo server: main loopUDP server: recvfrom(), sendto()UDP client: socket(), gethostbyname()UDP client: sendto(), recvfrom()Multiplexing I/O: select()TCP server based on select()Slide 41Slide 42For more infoInternetworking 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 a clientAFS 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 buf to 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 socket is 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
View Full Document