Network programming Nov 16, 2000Client-server programming modelClientsUsing ports to identify servicesServersServer examplesThe two basic ways that clients and servers communicateLinux file I/O: open()Linux file I/O: read()File I/O: write()Berkeley Sockets InterfaceWhat is a socket?Key data structuresKey data structuresEcho client: prologueEcho client: socket()Echo client: gethostbyname()Echo client: connect()Echo client: read(), write(), close()Echo server: prologueEcho server: socket()Echo server: setsockopt()Echo server: init socket addressTCP echo server: bind()Echo server: listen()Echo server: main loopEcho server: accept()Echo server: identifying clientEcho server: read()Echo server: write()Testing the echo server with telnetRunning the echo client and serverFor more infoNetwork 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 = 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 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 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 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 often referred 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 file See /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 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 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 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 F’00– 10 –class24.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 F’00– 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 F’00– 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 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.CS 213 F’00– 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 F’00– 14 –class24.pptKey data structures Defined in /usr/include/netdb.hHostent is
View Full Document