#include #include #include #include #include #include #include extern time_t time() ; int maxlives = 12 ; char *word[] = { #include "words" } ; #define NUM_OF_WORDS (sizeof(word)/sizeof(word[0])) #define MAXLEN 80 #define HANGMAN_TCP_PORT 1066 main() { int sock, fd, client_len; struct sockaddr_in server, client ; srand((int)time((long *)0)) ; sock=socket(AF_INET, SOCK_STREAM, 0) ; if (sock<0) { perror("creating stream socket"); exit(1) ; } server.sin_family = AF_INET ; server.sin_addr.s_addr = htonl(INADDR_ANY) ; server.sin_port = htons(HANGMAN_TCP_PORT) ; if (bind(sock, (struct sockaddr *) &server, sizeof (server)) < 0) { perror("binding socket") ; exit(2) ; } listen(sock, 5) ; while (1) { client_len = sizeof(client) ; if ((fd = accept(sock, (struct sockaddr *) &client, &client_len)) < 0) { perror("accepting connection"); exit(3) ; } play_hangman(fd,fd) ; close(fd) ; } } play_hangman(int in, int out) { char *whole_word, part_word[MAXLEN], guess[MAXLEN], outbuf[MAXLEN] ; int lives = maxlives ; int game_state = 'I' ; int i, good_guess, word_length ; char hostname[MAXLEN] ; gethostname(hostname, MAXLEN) ; sprintf(outbuf, "Playing hangman on host %s:\n\n", hostname) ; write(out, outbuf, strlen(outbuf)) ; whole_word = word[rand() % NUM_OF_WORDS] ; word_length = strlen(whole_word) ; syslog(LOG_USER|LOG_INFO, "hangman server chose word %s",whole_word) ; for(i=0; i