Network ProgrammingClient-Server TransactionsProgrammer’s View of Internet1. IP Addresses2. Domain Naming System (DNS)3. Internet ConnectionsClientsUsing Ports to Identify ServicesServersServer ExamplesSockets InterfaceOverview of Sockets InterfaceSocketsSocket Address StructuresEcho Client Main RoutineEcho Client: open_clientfdEcho Client: open_clientfd (getaddrinfo)Echo Client: open_clientfd (socket)Echo Client: open_clientfd (connect)Echo Server: Main RoutineEcho Server: open_listenfdEcho Server: open_listenfd (getaddrinfo)Echo Server: open_listenfd (socket)Echo Server: open_listenfd (setsockopt)Echo Server: open_listenfd (bind)Echo Server: open_listenfd (listen)Echo Server: Main LoopEcho Server: acceptEcho Server: accept IllustratedConnected vs. Listening DescriptorsEcho Server: Identifying ClientEcho Server: echoTesting Servers Using telnetTesting Echo Server With telnetRunning Echo Client and ServerOne More Important FunctionFor More InformationNetwork ProgrammingNetwork ProgrammingTopicsTopicsProgrammer’s view of the Internet (review)Sockets interfaceWriting clients and serversCS 105“Tour of the Black Holes of Computing!”– 2 –CS 105Client-Server TransactionsClient-Server TransactionsClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceEvery network application is based on client-server Every network application is based on client-server model:model:Server process and one or more client processesServer manages some resource.Server provides service by manipulating resource for clientsNote: clients and servers are processes running on hosts (can be the same or different hosts)– 3 –CS 105Programmer’s View of InternetProgrammer’s View of Internet1. Hosts are mapped to set of 32-bit or 128-bit 1. Hosts are mapped to set of 32-bit or 128-bit IP IP addressesaddresses134.173.42.22001:1878:301:902:214:22ff:fe7c:883f2. IP addresses are mapped to identifiers called Internet 2. IP addresses are mapped to identifiers called Internet domain namesdomain names134.173.42.2 maps to www.cs.hmc.edu In general, mapping is many-to-many3. Process on one Internet host can communicate with 3. Process on one Internet host can communicate with process on another over a process on another over a connectionconnection– 4 –CS 1051. IP Addresses1. IP AddressesComputers are identified by Computers are identified by IP addressesIP addressesTwo flavors: IPv4 (old) and IPv6 (new)Two flavors: IPv4 (old) and IPv6 (new)Both are stored in an Both are stored in an IP address struct IP address struct of appropriate of appropriate typetypein_addr for IPv4in6_addr for IPv6Details don’t matter; library functions usually hide themDetails don’t matter; library functions usually hide them– 5 –CS 1052. Domain Naming System (DNS)2. Domain Naming System (DNS)Internet maintains mapping between IP addresses and Internet maintains mapping between IP addresses and domain names in huge worldwide distributed database domain names in huge worldwide distributed database called called DNSDNSConceptually, programmers can view DNS database as collection of millions of host entry structures:Functions for retrieving host entries from DNS:Functions for retrieving host entries from DNS:getaddrinfo: query key is a DNS domain namegetnameinfo: 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 or AF_INET6) */ int h_length; /* length of an address, in bytes */ char **h_addr_list; /* null-terminated array of in*_addr structs */ };– 6 –CS 1053. Internet Connections3. Internet ConnectionsConnection socket pair(128.2.194.242:51213, 134.173.42.2:80)Server(port 80)ClientClient socket address128.2.194.242:51213Server socket address134.173.42.2:80Client host address128.2.194.242 Server host address134.173.42.2Clients and servers communicate by sending streams Clients and servers communicate by sending streams of bytes over of bytes over connectionsconnectionsConnections are point-to-point, full-duplex (2-way Connections are point-to-point, full-duplex (2-way communication), and reliablecommunication), and reliableNote: 51213 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web servers– 7 –CS 105ClientsClientsExamples of client programsExamples of client programsWeb browsers, ftp, telnet, sshHow does a client find the server?How does a client find the server?IP address in server socket address identifies host (more precisely, an adapter on the host)(Well-known) port in server socket address identifies service, and thus implicitly identifies server process that provides itExamples of well-known portsPort 7: Echo serverPort 23: Telnet serverPort 25: Mail serverPort 80: Web server– 8 –CS 105Using Ports to Identify ServicesUsing Ports to Identify ServicesWeb server(port 80)Client hostServer host 134.173.42.2Echo server(port 7)Service request for134.173.42.2:80(i.e., Web server)Web server(port 80)Echo server(port 7)Service request for134.173.42.2:7(i.e., echo server)KernelKernelClientClient– 9 –CS 105ServersServersServers are long-running processes (daemons).Servers are long-running processes (daemons).Created at boot time (typically) by init process (process 1)Run continuously until machine is turned offOr spawned by inetd in response to connection to portEach server waits for requests to arrive on well-known Each server waits for requests to arrive on well-known port associated with that particular serviceport associated with that particular servicePort 7: echo serverPort 22: ssh serverPort 25: mail serverPort 80: HTTP serverMachine that runs a server process is also often referred Machine that runs a server process is also often referred to as a “server”to as a “server”– 10 –CS 105Server 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: filesService: stores and retrieve filesSsh server (22)Ssh server (22)Resource: terminalService: proxies a terminal on the server machineMail
View Full Document