Network programming Nov 16, 2000Topics• Client-server model• Sockets interface• Echo client and serverclass24.ppt15-213“The course that gives CMU its Zip!”CS 213 F’00– 2 –class24.pptClient-server programming modelClient + server = distributedcomputingClient & server are bothprocessesServer manages a resourceClient makes a request for aservice• request may involve aconversation according to someserver protocolServer provides service bymanipulating the resourceon behalf of client and thenreturning a responseclient serverrequestclient serverresponseclient serverprocessrequestCS 213 F’00– 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 identifiesthe machine.»dotted decimal form: 0x8002C2F2 = 128.2.194.242–The port is positive integer associated with a service (and thus aserver) on that machine.»port 7: echo server»port 23: telnet server»port 25: mail server»port 80: web serverCS 213 F’00– 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)CS 213 F’00– 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 oftenreferred to as a “server”.CS 213 F’00– 6 –class24.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 fileSee /etc/services for a comprehensive listof the services available on a Linux machine.CS 213 F’00– 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 unreliablechunks.• can be lost or arrive out oforder.• akin to using surface mail.• faster but less robust.We will only discussconnections.client server... , Bk, Bk-1, ... , B1, B0B0, B1, ..., Bk-1, Bk, ...connectionclient serverdgram dgramdgramdgramCS 213 F’00– 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);}CS 213 F’00– 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 intomemory 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 F’00– 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 F’00– 11 –class24.pptBerkeley Sockets InterfaceCreated in the early 80’s as part of the original Berkeleydistribution of Unix that contained an early version ofthe Internet protocols.Provides a user-level interface to the network.Underlying basis for all Internet applications.Based on client/server programming model.CS 213 F’00– 12 –class24.pptWhat is a socket?A socket is a descriptor that lets an applicationread/write from/to the network.• Key idea: Linux uses the same abstraction for both file I/O andnetwork I/O.Clients and servers communicate with each by readingfrom 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 ishow the application “opens” the socket descriptors.CS 213 F’00– 13 –class24.pptKey data structuresDefined in /usr/include/netinet/in.hInternet-style sockets are characterized by a 32-bit IPaddress 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 F’00– 14 –class24.pptKey data structures Defined in /usr/include/netdb.hHostent is a DNS host entry that associates a domainname (e.g., cmu.edu) with an IP addr (128.2.35.186)• DNS (Domain Name Service) is a world-wide distributed database ofdomain name/IP address mappings.• Can be accessed from user programs using gethostbyname()[domain name to IP address] or gethostbyaddr() [IP address todomain 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 F’00– 15 –class24.pptEcho client: prologue/* * error - wrapper for perror
View Full Document