stop recvfrom() after certain time

1.1k Views Asked by At

I wanna wait for a constant amount of time(like 3 seconds) for recvfrom() to receive some data, and I wanna exit my program if it didn't receive anything (in this 3 seconds) how do that ?

1

There are 1 best solutions below

0
On

Call setsockopt() with the SO_RCVTIMEO option to set a read timeout on the socket. If the timeout fires, recvfrom() will return -1 with errno == EAGAIN/EWOULDBLOCK.