I'm running this piece of code several times (for a non-blocking recvfrom
on a UDP socket):
struct timeval read_timeout;
read_timeout.tv_sec = 0;
read_timeout.tv_usec = 1000;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof read_timeout);
ssize_t n = recvfrom(sockfd, recvline, sizeof(recvline), 0, NULL, NULL);
if (n < 0) {
perror("recvfrom");
return -1;
} else // ... normal usage
Sometimes the program stops and I get the following error from perror
recvfrom: Resource temporarily unavailable
What could the issue be?
Just modify the code to do what you want. When you get an error, instead of calling perror
unconditionally, check if it's a timeout. If it's not a timeout, then call perror
and return. If it's a timeout, do whatever it is you want to do when you get a timeout.