// Student: Steve Fiorelli // Professor: Victor Ralevich // Due: Friday December 7th, 2007 // Unix Systems Programming course // Run server then run client code /* server process */ #include #include #include #include #include #include #define SIZE sizeof(struct sockaddr_in) void catcher(int sig); int newsockfd; main() { int sockfd; char c; struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY}; static struct sigaction act; act.sa_handler = catcher; sigfillset(&(act.sa_mask)); sigaction(SIGPIPE, &act, NULL); /* set up the transport end point */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket call failed"); exit(1); } /* bind an address to the end point */ if (bind(sockfd, (struct sockaddr *) &server, SIZE) == -1) { perror("bind call failed"); exit(1); } /* start listening for incoming connections */ if (listen(sockfd, 5) == -1) { perror("listen call failed"); exit(1); } // set alarm so that server doesn't sit waiting forever alarm(15); for(;;) { printf("Loop started.\n"); /*accept a connection */ // keeps sitting till connection if ((newsockfd = accept(sockfd, NULL, NULL)) == -1) { perror("accept call failed"); continue; } /* spawn a child to deal with the connection */ if (fork() == 0) { // Client connections get 10 seconds before expiry alarm(10); while (recv(newsockfd, &c, 1, 0) > 0) { c = toupper(c); send(newsockfd, &c, 1, 0); // When passing data client connection gets more time alarm(10); } /* when client is no longer sending information the socked can be closed and the child process terminated */ close(newsockfd); exit(0); } //added in else { // 15 second time added to parent once connection made alarm(15); } /* parent doesn't need the newsockfd */ close(newsockfd); } } void catcher (int sig) { close (newsockfd); exit(0); }