Networking in C · advanced · ~10 min
Cap blocking operations with a deadline.
By default read/recv on a blocking socket can wait forever. Three ways to bound it:
setsockopt(SO_RCVTIMEO) for read timeout (also SO_SNDTIMEO).select/poll with a timeout.alarm(seconds) (older, less flexible).struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);