#include #include #include #include #include #include #include #include #include #include #include #include int skt; // Global. This is the socket that the server will listen on pthread_mutex_t new_client_mutex; void close_server(int signal_number){ printf("Closing down the server\n"); close(skt); } char *words[128000]; size_t word_count = 0; char dictionary_contents[1024*1024]; void load_dictionary(){ char *filename = "/usr/share/dict/american-english"; int fd = open(filename, O_RDONLY); if(fd == -1){ perror("open"); exit(1); } size_t readlen = read(fd, dictionary_contents, 1024*1024); char *prev_start = dictionary_contents; for(int i = 0; i < readlen; i++){ if(dictionary_contents[i] == '\n'){ words[word_count++] = prev_start; dictionary_contents[i] = 0; prev_start = dictionary_contents + i + 1; } } } char* random_word(){ size_t t = random() % word_count; return words[t]; } void *handle_client(void* arg){ char word_request; int client_fd = (long int)arg; while(1){ size_t readlen = read(client_fd, &word_request, 1); if(word_request == '0') break; if(readlen == 1){ for(int i = 0; i < word_request - '0'; i++){ char* rw = random_word(); printf("Sending word: %s (%d)\n", rw, strlen(rw)); write(client_fd, rw, strlen(rw)); write(client_fd, "\n", 1); } } } close(client_fd); } int main(int argc, char ** argv){ srandom((long int)(&argc)); load_dictionary(); struct sockaddr_in sad; sad.sin_port = htons(5142); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; skt = socket(AF_INET, SOCK_STREAM, 0); if(skt == -1){ perror("socket"); return 1; } struct sigaction close_action; close_action.sa_handler = close_server; if(sigaction(SIGINT, &close_action, 0)){ perror("sigaction"); return 1; } if( bind(skt, (struct sockaddr *)(&sad), sizeof(struct sockaddr_in)) ){ perror("bind"); return 1; } if( listen(skt, 5) ){ perror("listen"); return 1; } int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); for(;;){ client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); if(client_fd == -1){ perror("accept"); break; } printf("Connection made from address %s\n", inet_ntoa(client_address.sin_addr)); // start a new thread for the new client pthread_t T; pthread_create(&T, 0, handle_client, (void*)(long int) client_fd); } return 0; }