Page 1Network ProgrammingApril 20, 2004Network ProgrammingApril 20, 2004TopicsTopics Programmer’s view of the Internet (review) Sockets interface Writing clients and servers15-213“The course that gives CMU its Zip!”class26.ppt– 2 –15-213, S’04A Client-Server TransactionA Client-Server TransactionClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceMost network applications are based on the clientMost network applications are based on the client--server model:server model: A server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients.Note: clients and servers are processes running on hosts (can be the same or different hosts).– 3 –15-213, S’04A Programmer’s View of the InternetA Programmer’s View of the Internet1. Hosts are mapped to a set of 321. Hosts are mapped to a set of 32--bit bit IP addressesIP addresses.. 128.2.203.179 In IP V6, Host addresses are 64 bit: at some point in the future a transition from IP V4 to IP V6 will happen.2. The set of IP addresses is mapped to a set of 2. The set of IP addresses is mapped to a set of identifiers called Internet identifiers called Internet domain namesdomain names.. 128.2.203.179 is mapped to www.cs.cmu.edu3. A process on one Internet host can communicate 3. A process on one Internet host can communicate with a process on another Internet host over a with a process on another Internet host over a connectionconnection..– 4 –15-213, S’041. IP Addresses1. IP Addresses3232--bit IP addresses are stored in an bit IP addresses are stored in an IP address IP address structstruct IP addresses are always stored in memory in network byte order (big-endian byte order) True in general for any integer transferred in a packet header from one machine to another. E.g., the port number used to identify an Internet connection./* Internet address structure */struct in_addr {unsigned int s_addr; /* network byte order (big-endian) */};Handy network byte-order conversion functions:htonl: convert long int from host to network byte order.htons: convert short int from host to network byte order.ntohl: convert long int from network to host byte order.ntohs: convert short int from network to host byte order.Page 2– 5 –15-213, S’042. Domain Naming System (DNS)2. Domain Naming System (DNS)The Internet maintains a mapping between IP addresses The Internet maintains a mapping between IP addresses and domain names in a huge worldwide distributed and domain names in a huge worldwide distributed database called database called DNSDNS.. Conceptually, programmers can view the DNS database as a collection of millions of host entry structures:Functions for retrieving host entries from DNS:Functions for retrieving host entries from DNS: gethostbyname: query key is a DNS domain name. gethostbyaddr: query key is an IP address./* DNS host entry structure */ struct hostent { char *h_name; /* official domain name of host */ char **h_aliases; /* null-terminated array of domain names */ int h_addrtype; /* host address type (AF_INET) */ int h_length; /* length of an address, in bytes */ char **h_addr_list; /* null-terminated array of in_addr structs */ }; – 6 –15-213, S’043. Internet Connections3. Internet ConnectionsConnection 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.242Server host address208.216.181.15Clients and servers communicate by sending streams Clients and servers communicate by sending streams of bytes over of bytes over connectionsconnections..Connections are pointConnections are point--toto--point, fullpoint, full--duplex (2duplex (2--way way communication), and reliable.communication), and reliable.Note: 51213 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web servers– 7 –15-213, S’04ClientsClientsExamples of client programsExamples of client programs Web browsers, ftp, telnet, sshHow does a client find the server?How does a client find the server? The IP address in the server socket address identifies the host (more precisely, an adapter on the host) The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. Examples of well know ports Port 7: Echo server Port 22: Secure Shell (ssh) server (daemon) Port 25: Mail server Port 80: Web server– 8 –15-213, S’04Using Ports to Identify ServicesUsing Ports to Identify ServicesWeb server(port 80)Client hostServer host 128.2.194.242Echo server(port 7)Service request for128.2.194.242:80(i.e., the Web server)Web server(port 80)Echo server(port 7)Service request for128.2.194.242:7(i.e., the echo server)KernelKernelClientClientPage 3– 9 –15-213, S’04ServersServersServers are longServers are long--running processes (daemons).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 wellEach server waits for requests to arrive on a well--known known port associated with a particular service.port associated with a particular service. Port 7: echo server Port 22: ssh server Port 25: mail server Port 80: HTTP serverA machine that runs a server process is also often A machine that runs a server process is also often referred to as a “server.”referred to as a “server.”– 10 –15-213, S’04Server ExamplesServer ExamplesWeb server (port 80)Web server (port 80) Resource: files/compute cycles (CGI programs) Service: retrieves files and runs CGI programs on behalf of the clientFTP server (20, 21, depreciated)FTP server (20, 21, depreciated) Resource: files Service: stores and retrieve filesSSH server (22)SSH server (22) Resource: terminal Service: proxies a terminal on the server machine over an excrypted and authenticated connection. Also supports file transfers, forwarding of ports, and X11 sessions.Mail server (25)Mail server (25) Resource: email “spool” file Service: stores mail messages in spool file See /etc/services for a comprehensive list of the services available on a Linux
View Full Document