Network ProgrammingNov. 6, 2008Network ProgrammingNov. 6, 2008TopicsTopics Peeking at Internet traffic Programmer’s view of the Internet (review) Sockets interface Writing clients and servers Understanding protocol15-213“The course that gives CMU its Zip!”class20.ppt–2–15-213, F’08A 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, F’08A 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.1792. 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, F’081. 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.z 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 uint32_t from host to network byte order.htons: convert uint16_t from host to network byte order.ntohl: convert uint32_t from network to host byte order.ntohs: convert uint16_t from network to host byte order.–5–15-213, F’082. 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, F’083. 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, F’08ClientsClientsExamples 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 portsz Port 7: Echo serverz Port 23: Telnet serverz Port 25: Mail serverz Port 80: Web server–8–15-213, F’08Using 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)KernelKernelClientClient–9–15-213, F’08ServersServersServers 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 23: telnet 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 referred to as a ““server.server.””–10–15-213, F’08Server 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)FTP server (20, 21) Resource: files Service: stores and retrieve filesTelnet server (23)Telnet server (23) Resource: terminal Service: proxies a terminal on the server machineMail 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 machine.–11–15-213, F’08Sockets InterfaceSockets InterfaceCreated in the early 80Created in the early 80’’s as part of the original Berkeley s as part of the original Berkeley distribution of Unix that contained an early version of distribution of Unix that
View Full Document