15 213 The course that gives CMU its Zip Network Programming Nov 11 2003 Topics Programmer s view of the Internet review Sockets interface Writing clients and servers A Client Server Transaction Most network applications are based on the clientserver model A server process and one or more client processes Server manages some resource Server provides service by manipulating resource for clients 1 Client sends request Client process 4 Client handles response Server process 3 Server sends response Resource 2 Server handles request Note clients and servers are processes running on hosts can be the same or different hosts 2 15 213 F 03 A Programmer s View of the Internet 1 Hosts are mapped to a set of 32 bit IP addresses 128 2 203 179 2 The set of IP addresses is mapped to a set of identifiers called Internet domain names 128 2 203 179 is mapped to www cs cmu edu 3 A process on one Internet host can communicate with a process on another Internet host over a connection 3 15 213 F 03 1 IP Addresses 32 bit IP addresses are stored in an IP address struct 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 4 15 213 F 03 2 Domain Naming System DNS The Internet maintains a mapping between IP addresses and domain names in a huge worldwide distributed database called DNS Conceptually programmers can view the DNS database as a collection of millions of host entry structures DNS host entry structure struct hostent char h name char h aliases int h addrtype int h length char h addr list official domain name of host null terminated array of domain names host address type AF INET length of an address in bytes null terminated array of in addr structs Functions for retrieving host entries from DNS 5 gethostbyname query key is a DNS domain name gethostbyaddr query key is an IP address 15 213 F 03 3 Internet Connections Clients and servers communicate by sending streams of bytes over connections Connections are point to point full duplex 2 way communication and reliable Client socket address 128 2 194 242 51213 Client Server socket address 208 216 181 15 80 Connection socket pair 128 2 194 242 51213 208 216 181 15 80 Server port 80 Client host address 128 2 194 242 Server host address 208 216 181 15 Note 51213 is an ephemeral port allocated 6 by the kernel Note 80 is a well known port associated with Web servers 15 213 F 03 Clients Examples of client programs Web browsers ftp telnet ssh 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 23 Telnet server Port 25 Mail server Port 80 Web server 7 15 213 F 03 Using Ports to Identify Services Server host 128 2 194 242 Client host Service request for 128 2 194 242 80 i e the Web server Client Web server port 80 Kernel Echo server port 7 Client Service request for 128 2 194 242 7 i e the echo server Web server port 80 Kernel Echo server port 7 8 15 213 F 03 Servers 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 port associated with a particular service Port 7 echo server Port 23 telnet server Port 25 mail server Port 80 HTTP server A machine that runs a server process is also often referred to as a server 9 15 213 F 03 Server Examples Web server port 80 Resource files compute cycles CGI programs Service retrieves files and runs CGI programs on behalf of the client FTP server 20 21 Resource files Service stores and retrieve files Telnet server 23 See etc services for a comprehensive list of the services available on a Linux machine Resource terminal Service proxies a terminal on the server machine Mail server 25 Resource email spool file Service stores mail messages in spool file 10 15 213 F 03 Sockets Interface Created 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 11 15 213 F 03 Overview of the Sockets Interface Client Server socket socket bind open listenfd open clientfd listen connect Connection request rio writen rio readlineb rio readlineb close 12 accept rio writen EOF Await connection request from next client rio readlineb close 15 213 F 03 Sockets What is a socket To the kernel a socket is an endpoint of communication To an application a socket is a file descriptor that lets the application read write from to the network Remember All Unix I O devices including networks are modeled as files Clients and servers communicate with each other by reading from and writing to socket descriptors The main distinction between regular file I O and socket I O is how the application opens the socket descriptors 13 15 213 F 03 Socket Address Structures Generic socket address For address arguments to connect bind and accept Necessary only because C did not have generic void pointers when the sockets interface was designed struct sockaddr unsigned short sa family char sa data 14 protocol family address data Internet specific socket address Must cast sockaddr in to sockaddr for connect bind and accept struct sockaddr in unsigned short sin family unsigned short sin port struct in addr sin addr unsigned char sin zero 8 14 address family always AF INET port num in network byte order IP addr in network byte order pad to sizeof struct sockaddr 15 213 F 03 Echo Client Main Routine include csapp h usage echoclient host port int main int argc char argv int clientfd port char host buf MAXLINE rio t rio host argv 1 port atoi argv 2 clientfd Open clientfd host port Rio readinitb rio clientfd
View Full Document