15-213 Recitation 10 Greg ReshkoOutlineInternet connectionBerkeley Sockets InterfaceWhat is a socket?Key data structuresKey data structuresOverview of the Sockets InterfaceEcho clientEcho client: open_clientfd()Echo client: open_clientfd() (socket)Echo client: open_clientfd() (gethostbyname)Echo client: open_clientfd() (connect)Echo serverEcho server: open_listenfd()Slide 16Echo server: open_listenfd() (socket)Echo server: open_listenfd() (setsockopt)Echo server: open_listenfd()Echo server: open_listenfd() (bind)Echo server: open_listenfd (listen)Echo server: main loopEcho server: accept()accept()Echo server: identifying the clientEcho server: echo()Q&A15-213 Recitation 10Greg ReshkoOffice Hours: Wed 2:00-3:00PMApril 28th, 2003OutlineSockets InterfaceEcho Client/ServerFCEsInternet connectionClients and se rvers com munic ate by sendin g stream s o f bytes over c onnec tions :point -to-point, full-du plex, and reliabl eA socke t is an endpoi nt of a con necti onSock et add ress is an IPaddress:port pairA port is a 16 -bit i nteger that identif ies a proce ss:ephemeral po rt: ass igne d autom atical ly on c li ent when clien t makes a con nectio n reque stwel l-know n po rt: ass ocia ted wi th som e servic e provide d by a se rver (e.g., port 8 0 is as so ciate d with Web servers)A connec tion is uniq uely id entifie d by the s ocket addres ses of its endpoin ts (s ocke t pair)(cliaddr:cliport, servaddr:servport)Berkeley Sockets InterfaceCreated 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.What is a socket?A socket is a descriptor that lets an application read/write from/to the networkKey idea: Unix uses the same abstraction for both file I/O and network I/OClients and servers communicate with each by reading from and writing to socket descriptorsUsing regular Unix read and write I/O functionsThe main difference between file I/O and socket I/O is how the application “opens” the socket descriptorsKey data structuresDefined in /usr/include/netinet/in.hInternet-style sockets are characterized by a 32-bit IP address and a port/* 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; /* Address family (AF_INET) */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* IP address */ unsigned char sin_zero[...]; /* Pad to sizeof “struct sockaddr” */};Key data structures Defined in /usr/include/netdb.hhostent is a DNS host entry that associates a domain name (e.g., cmu.edu) with an IP addr (128.2.35.186)Can be accessed from user programs gethostbyname() [domain name key]gethostbyaddr() [IP address key]Can also be accessed from the shell using nslookup or dig/* Domain Name Service (DNS) host entry */struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */}Overview of the Sockets InterfaceClientServersocket socketbindlistenacceptreadlinereadlinewritenclosereadlineconnectwritencloseconnectionrequestEOFAwait connectionrequest fromnext clientopen_listenfdopen_clientfdEcho clientint main(int argc, char **argv){ int clientfd, port; char *host, buf[MAXLINE]; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = open_clientfd(host, port); while (Fgets(buf, MAXLINE, stdin) != NULL) { Writen(clientfd, buf, strlen(buf)); Readline(clientfd, buf, MAXLINE); Fputs(buf, stdout); } Close(clientfd);}Echo client: open_clientfd()int open_clientfd(char *hostname, int port){ int clientfd; struct hostent *hp; struct sockaddr_in serveraddr; clientfd = Socket(AF_INET, SOCK_STREAM, 0); /* fill in the server's IP address and port */ hp = Gethostbyname(hostname); bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* establish a connection with the server */ Connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)); return clientfd;}Echo client: open_clientfd() (socket)The client creates a socket that will serve as the endpoint of an Internet (AF_INET) connection (SOCK_STREAM).socket() returns an integer socket descriptor.int clientfd; /* socket descriptor */clientfd = Socket(AF_INET, SOCK_STREAM, 0);Echo client: open_clientfd() (gethostbyname)The client builds the server’s Internet address. int clientfd; /* socket descriptor */ struct hostent *hp; /* DNS host entry */ struct sockaddr_in serveraddr; /* server’s IP address */ typedef struct sockaddr SA; /* generic sockaddr */... /* fill in the server's IP address and port */ hp = Gethostbyname(hostname); bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port);Echo client: open_clientfd() (connect)Then the client creates a connection with the serverThe client process suspends (blocks) until the connection is created with the server.At this point the client is ready to begin exchanging messages with the server via Unix I/O calls on the descriptor sockfd. int clientfd; /* socket descriptor */ struct sockaddr_in serveraddr; /* server address */... /* establish a connection with the server */ Connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr));Echo serverint main(int argc, char **argv) { int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp; port = atoi(argv[1]); /* the server listens on a port passed on the command line */ listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr);
View Full Document