Error handling sockets wrappers 15 213 The course that gives CMU its Zip To simplify our code we will use error handling wrappers of the form Concurrent Servers int Accept int s struct sockaddr addr int addrlen int rc accept s addr addrlen if rc 0 unix error Accept return rc December 7 2000 Topics Baseline iterative server Process based concurrent server Threads based concurrent server select based concurrent server void unix error char msg printf s s n msg strerror errno exit 0 class29 ppt class29 ppt Echo client revisited echoclient c A simple connection based echo client usage echoclient host port include ics h define BUFSIZE 1024 int main int argc char argv int sockfd struct sockaddr in serveraddr struct hostent server char hostname int portno char buf BUFSIZE client socket server socket addr struct server s DNS entry server s domain name server s port number check command line arguments if argc 3 fprintf stderr usage s hostname port n argv 0 exit 0 hostname argv 1 portno atoi argv 2 class29 ppt 3 CS 213 F 00 2 CS 213 F 00 Echo client cont create the socket sockfd Socket AF INET SOCK STREAM 0 initialize the server s socket address struct server Gethostbyname hostname bzero char serveraddr sizeof serveraddr serveraddr sin family AF INET bcopy char server h addr char serveraddr sin addr s addr server h length serveraddr sin port htons portno request a connection to the server Connect sockfd struct sockaddr serveraddr sizeof serveraddr class29 ppt 4 CS 213 F 00 open streamsock helper function Echo client cont int open streamsock int portno int listenfd optval 1 struct sockaddr in serveraddr get a message line from the user printf Please enter msg bzero buf BUFSIZE fgets buf BUFSIZE stdin create a socket descriptor listenfd Socket AF INET SOCK STREAM 0 Setsockopt listenfd SOL SOCKET SO REUSEADDR const void optval sizeof int send message line to server and read its echo Write sockfd buf strlen buf bzero buf BUFSIZE Read sockfd buf BUFSIZE printf Echo from server s buf Close sockfd exit 0 accept requests to any IP addr portno bzero char serveraddr sizeof serveraddr serveraddr sin family AF INET serveraddr sin addr s addr htonl INADDR ANY serveraddr sin port htons unsigned short portno Bind listenfd struct sockaddr serveraddr sizeof serveraddr Make it a listening socket ready to accept conn requests Listen listenfd 5 return listenfd class29 ppt 5 CS 213 F 00 class29 ppt Iterative servers server call connect call accept client 2 call connect ret connect ret accept write call read ret read close close call accept ret accept ret connect echoserveri c iterative echo server Usage echoserveri port include ics h define BUFSIZE 1024 void echo int connfd int main int argc char argv int listenfd connfd int portno struct sockaddr in clientaddr int clientlen sizeof struct sockaddr in call read write check command line args if argc 2 fprintf stderr usage s port n argv 0 exit 0 portno atoi argv 1 ret read close close class29 ppt 7 CS 213 F 00 Iterative echo server Iterative servers process one request at a time client 1 6 CS 213 F 00 class29 ppt 8 CS 213 F 00 Iterative echo server cont open the listening socket listenfd open streamsock portno main server loop while 1 connfd Accept listenfd struct sockaddr clientaddr clientlen echo connfd Close connfd Pros and cons of iterative servers simple can process only one request at a time one slow client can hold up thousands of others Example echo clients and server client 1 server client 2 call accept call connect ret connect echo read and echo a line from a client connection void echo int connfd int n char buf BUFSIZE bzero buf BUFSIZE n Read connfd buf BUFSIZE printf server received d bytes s n buf Write connfd buf strlen buf 9 User goes out to lunch Server blocks call read waiting for data from Client 1 Client 1 blocks waiting for user to type in data class29 ppt ret accept call fgets CS 213 F 00 Concurrent servers class29 ppt 10 call connect Client 2 blocks waiting to complete its connection request until after lunch CS 213 F 00 Example Concurrent echo server Concurrent servers process multiple requests concurrently The basic idea is to use multiple control flows to handle multiple requests client 1 server client 2 call accept call connect call connect ret connect ret accept call fgets Example concurrent server designs Fork a new child process for each request Create a new thread for each request Pre fork a pool of child processes to handle requests not discussed Pre create a pool of threads to handle requests not discussed Manually interleave the processing for multiple open connections Uses Linux select function to notice pending socket activity Form of application level concurrency User goes out to lunch Client 1 blocks waiting for user to type in data fork child 1 call accept call read ret accept fork child 2 call read 11 CS 213 F 00 class29 ppt write call read write close class29 ppt ret connect call fgets 12 end read close CS 213 F 00 Process based concurrent server echoserverp c A concurrent echo server based on processes Usage echoserverp port include ics h define BUFSIZE 1024 void echo int connfd void handler int sig Process based server cont Signal SIGCHLD handler parent must reap children main server loop while 1 for complete portability must restart if interrupted by call to SIGCHLD handler if connfd accept listenfd struct sockaddr clientaddr clientlen 0 if errno EINTR continue go back else unix error accept int main int argc char argv int listenfd connfd int portno struct sockaddr in clientaddr int clientlen sizeof struct sockaddr in if Fork 0 Close listenfd child closes its listening socket echo connfd child reads and echos input line Close connfd child is done with this client exit 0 child exits Close connfd parent must close connected socket if argc 2 fprintf stderr usage s port n argv 0 exit 0 portno atoi argv 1 listenfd open streamsock portno class29 ppt 13 CS 213 F 00 Reaping zombie children handler reaps children as they terminate void handler int sig pid t pid int stat while pid waitpid 1 stat WNOHANG 0 return class29 ppt 14 CS 213 F 00 Issues with process based design Server should restart accept call if it is interrupted by a transfer of control to the SIGCHLD handler not necessary for systems such as Linux that support Posix signal handling required for portability on some older Unix systems Server must reap zombie children to avoid fatal memory leak Question Why is the call to waitpid in a loop class29 ppt 15 CS 213 F 00
View Full Document