15 213 Internetworking II Network programming Topics April 20 2000 client server model Berkeley sockets TCP client and server examples UDP client and server examples I O multiplexing with select class26 ppt Internet protocol stack Berkeley sockets interface User application program FTP Telnet WWW email Unreliable best effort datagram delivery processprocess User datagram protocol UDP Unreliable best effort datagram delivery host host class26 ppt Transmission control protocol TCP Internet Protocol IP Reliable byte stream delivery processprocess Network interface ethernet hardware 2 Physical connection CS 213 S 00 UDP vs TCP User Datagram Protocol UDP unreliable datagrams from process to process thin veneer over IP similar to sending surface mail each message is an independent chunk of data datagram messages may not arrive or may arrive out of order faster than TCP requires no server state but ureliable Transmission Control Protocol TCP reliable byte stream from process to process complex implementation similar to placing a phone call no messages just a continuous stream of bytes over a connection bytes arrive in order slower and requires more resources but cleaner user semantics class26 ppt 3 CS 213 S 00 Berkeley Sockets Interface Created in the early 80 s as part of the original Berkeley distribution of Unix that contained the TCP IP protocol stack Provides user level interface to UDP and TCP Underlying basis for all Internet applications Based on client server programming model class26 ppt 4 CS 213 S 00 Client server programming model Client server distributed computing Client server are both processes Server manages a resource Client makes a request for a service client request server process request client server request may involve a conversation according to some server protocol Server provides service by manipulating the resource on behalf of client and then returning a response class26 ppt 5 client response server CS 213 S 00 Internet Servers Servers are long running processes daemons Created at boot time typically by the init process Run continuously until the machine is turned off Each server waits for either TCP connection requests or UDP datagrams to arrive on a wellknown port associated with a particular service port 7 echo server port 25 mail server port 80 http server A machine that runs a server process is also often referred to as a server class26 ppt 6 CS 213 S 00 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 resource terminal service proxies a terminal on the server machine Mail server 25 resource email spool file service stores mail messages in spool file class26 ppt 7 CS 213 S 00 Server examples cont DNS name server 53 resource distributed name database service distributed database lookup Whois server 430 resource second level domain name database e g cmu edu service database lookup Daytime 13 resource system clock service retrieves value of system clock DHCP server 67 resource IP addresses service assigns IP addresses to clients class26 ppt 8 CS 213 S 00 Server examples cont X server 177 resource display screen and keyboard service paints screen and accepts keyboard input on behalf of a client AFS file server 7000 resource subset of files in a distributed filesystem e g AFS NFS service retrieves and stores files Kerberos authentication server 750 resource tickets service authenticates client and returns tickets etc services file gives a comprehensive list for Linux machines class26 ppt 9 CS 213 S 00 File I O open Must open a file before you can do anything else open returns a small integer file descriptor fd 0 indicates that an error occurred predefined file descriptors 0 stdin 1 stdout 2 stderr int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 class26 ppt 10 CS 213 S 00 File I O read read allows a program to access the contents of file char buf 512 int fd file descriptor int nbytes number of bytes read open the file read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 read returns the number of bytes read from file fd nbytes 0 indicates that an error occurred if successful read places nbytes bytes into memory starting at address buf class26 ppt 11 CS 213 S 00 File I O write write allows a program to modify file contents char buf 512 int fd file descriptor int nbytes number of bytes read open the file write up to 512 bytes from buf to file fd if nbytes write fd buf sizeof buf 0 perror write exit 1 write returns the number of bytes written from buf to file fd nbytes 0 indicates that an error occurred class26 ppt 12 CS 213 S 00 What is a socket A socket is a descriptor that lets an application read write from to the network Unix uses the same abstraction for both file I O and network I O Clients and servers communicate with each other via TCP and UDP using the same socket abstraction applications read and write TCP byte streams by reading from and writing to socket descriptors applications read write UDP datagrams by reading from and writing to socket descriptors Main difference between file I O and socket I O is how the application opens the sock descriptors class26 ppt 13 CS 213 S 00 Key data structures Defined in usr include netinet in h Internet address struct in addr unsigned int s addr 32 bit IP address Internet style socket address struct sockaddr in unsigned short int sin family unsigned short int sin port struct in addr sin addr unsigned char sin zero Address family AF INET Port number IP address Pad to sizeof struct sockaddr Internet style sockets are characterized by a 32 bit IP address and a port class26 ppt 14 CS 213 S 00 Key data structures Defined in usr include netdb h Domain Name Service DNS struct hostent char h name char h aliases int h addrtype int h length char h addr list host entry official name of host alias list host address type length of address list of addresses Hostent is a DNS host entry that associates a domain name e g cmu edu with an IP addr 128 2 35 186 DNS is a world wide distributed database of domain name IP address mappings Can be accessed from user programs using gethostbyname domain name to IP address or gethostbyaddr IP address to domain name Can also be accessed from the shell using nslookup or dig class26 ppt 15 CS 213 S 00 TCP echo server prologue The server listens on a port
View Full Document