Network Programming Nov 11, 2003A Client-Server TransactionA Programmer’s View of the Internet1. IP Addresses2. Domain Naming System (DNS)3. Internet ConnectionsClientsUsing Ports to Identify ServicesServersServer ExamplesSockets InterfaceOverview of the Sockets InterfaceSocketsSocket Address StructuresEcho Client Main RoutineEcho Client: open_clientfdEcho Client: open_clientfd (socket)Echo Client: open_clientfd (gethostbyname)Echo Client: open_clientfd (connect)Echo Server: Main RoutineEcho Server: open_listenfdEcho 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: acceptEcho Server: accept IllustratedConnected vs. Listening DescriptorsEcho Server: Identifying the ClientEcho Server: echoTesting Servers Using telnetTesting the Echo Server With telnetRunning the Echo Client and ServerFor More InformationNetwork ProgrammingNov 11, 2003Network ProgrammingNov 11, 2003TopicsTopicsProgrammer’s view of the Internet (review)Sockets interfaceWriting clients and servers15-213“The course that gives CMU its Zip!”– 2 –15-213, F’03A Client-Server TransactionA Client-Server TransactionClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceMost network applications are based on the client-Most network applications are based on the client-server model:server model:A server process and one or more client processesServer 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’03A Programmer’s View of the InternetA Programmer’s View of the Internet1. Hosts are mapped to a set of 32-bit 1. Hosts are mapped to a set of 32-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.edu 3. 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’031. IP Addresses1. IP Addresses32-bit IP addresses are stored in an 32-bit IP addresses are stored in an IP address structIP address structIP 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.– 5 –15-213, F’032. 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’033. 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.242 Server 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 point-to-point, full-duplex (2-way Connections are point-to-point, full-duplex (2-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’03ClientsClientsExamples of client programsExamples of client programsWeb 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 portsPort 7: Echo serverPort 23: Telnet serverPort 25: Mail serverPort 80: Web server– 8 –15-213, F’03Using 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’03ServersServersServers are long-running processes (daemons).Servers 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 Each server waits for requests to arrive on a well-known port associated with a particular service.port associated with a particular service.Port 7: echo serverPort 23: telnet serverPort 25: mail serverPort 80: HTTP serverA machine that runs a server process is also often A machine
View Full Document