Network programming Nov 27, 2001Client-server modelClientsUsing ports to identify servicesServersServer examplesThe two basic ways that clients and servers communicateInternet connections (review)Anatomy of an Internet connection (review)Berkeley Sockets InterfaceWhat is a socket?Key data structuresKey data structuresOverview of the Sockets InterfaceEcho clientEcho client: open_clientfd()Echo client: open_clientfd() (socket)Echo client: open_clientfd() (gethostbyname)Echo client: open_clientfd() (connect)Echo serverEcho server: open_listenfd()Echo server: open_listenfd() (cont)Echo server: open_listenfd() (socket)Echo server: open_listenfd() (setsockopt)Echo server: open_listenfd() (initialize socket address)Echo server: open_listenfd() (bind)Echo server: open_listenfd (listen)Echo server: main loopEcho server: accept()accept() illustratedEcho server: identifying the clientEcho server: echo()Testing servers using telnetTesting the echo server with telnetRunning the echo client and serverFor detailed infoNetwork programming Nov 27, 2001Topics•Client-server model•Sockets interface•Echo client and serverclass26.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class26.pptClient-server modelclientprocessserverprocess1. client sends request2. server handlesrequest3. server sends response4. client handlesresponseresourceEvery network application is based on the client-server model:•Application is a server process and one or more client processes•Server manages some resource, and provides service by manipulating resource for clients.•Client makes a request for a service–request may involve a conversation according to some server protocol•Server provides service by manipulating the resource on behalf of client and then returning a responseCS 213 F’01– 3 –class26.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 host (adapter).»dotted decimal form: 0x8002C2F2 = 128.2.194.242–The port is positive integer associated with a service (and thus a server process) on that machine. »port 7: echo server»port 23: telnet server»port 25: mail server»port 80: web serverCS 213 F’01– 4 –class26.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’01– 5 –class26.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’01– 6 –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 file See /etc/services for a comprehensive listof the services available on a Linux machine.CS 213 F’01– 7 –class26.pptThe two basic ways that clients and servers communicateConnections (TCP):•reliable two-way byte-stream.•looks like a file.•akin to placing a phone call.•slower but more robust.Datagrams (UDP): •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’01– 8 –class26.pptInternet connections (review)Clients and serve rs communicate by sending stre ams of bytes over connect ions:•point-to-point, full-du plex, and reli able.A socket is an endpoint of a connection•Socket address is an IPaddress:port pairA port is a 16-bit integer that identifies a process:•ephemeral por t: assigned automatically on client w hen client makes a conn ection requ est•well-known port: associated with some service provided by a server (e.g ., po rt 80 is associated with Web servers)A connection is uniquely identified by the socket addresse s of its endpoints (socket pair)•(cliaddr:clipor t, ser vaddr: servpo rt)CS 213 F’01– 9 –class26.pptAnatomy of an Internet connection(review)connection socket pair(128.2.194.242 :51213, 208.216.181.15:80)server(port 80)clientclient socket address128.2.194.242:51213server socket address208.216.181.15:80client host address128.2.194.242 server host address208.216.181.15CS 213 F’01– 10 –class26.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’01– 11 –class26.pptWhat is a socket?A socket is a descriptor that lets an application read/write from/to the network.•Key idea: Unix 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 Unix 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’01– 12 –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 F’01– 13
View Full Document