Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *thread_handle_connection(void *arg) {
- char buffer[MAX_MSG_SIZE]; // Receive buffer
- int bytes_read;
- do {
- // If there aren't any connections, sleep and recheck every second
- while(!num_connections && !term_requested) {
- sleep(1);
- }
- // Lock out connections queue and grab the first one
- pthread_mutex_lock(&queue_mutex);
- int connectionfd = remove_connection_from_queue();
- pthread_mutex_unlock(&queue_mutex);
- if(-1 == connectionfd) {
- continue;
- }
- // pthread_barrier_wait(&barrier); // Barrier for threads - for testing only
- // Read up to 1024 bytes from the client
- bytes_read = recv(connectionfd, buffer, MAX_MSG_SIZE - 1, 0);
- // If the data was read successfully
- if(bytes_read > 0) {
- // Add a terminating NULL character and print the message received
- buffer[bytes_read] = '