Why does my while loop not print the print statements I want before terminating? None of my print statements print in the terminal when my program exits. I can't see any other places where the loop might exit.
while (1) {
int num_read;
// add the fd value into the fd_set value
FD_SET(sock_fd, &read_fds);
FD_SET(STDIN_FILENO, &read_fds);
// choose which fds to watch
select(sock_fd + 1, &read_fds, '\0', '\0', '\0');
if (FD_ISSET(STDIN_FILENO, &read_fds)) { //check whether the fd value is in the fd_set
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
printf("Print statement before terminating");
break;
}
buf[num_read] = '\0'; // Just because I'm paranoid
int num_written = write(sock_fd, buf, num_read);
if (num_written != num_read) {
perror("client: write");
printf("Print statement before terminating");
close(sock_fd);
printf("Print statement before terminating");
exit(1);
}
}
if (FD_ISSET(sock_fd, &read_fds)) { //the socket with the server
num_read = read(sock_fd, buf, BUF_SIZE);
buf[num_read] = '\0';
printf("%s", buf);
}
}
printf("Print statement before terminating");
close(sock_fd);
printf("Print statement before terminating");
return 0;
}