SELECT()
this is used to wait for an event on one or more sockets
// 10. select() - Wait for an event on the socket
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(client_socket, &readfds);
struct timeval timeout = {5, 0}; // 5 seconds timeout
if (select(client_socket + 1, &readfds, NULL, NULL, &timeout) < 0) {
perror("select");
} else if (FD_ISSET(client_socket, &readfds)) {
printf("Data is available to read.\n");
}
Last updated