If you use the socket in blocking mode, then using select()
to wait for data to arrive before then calling recvfrom()
is one (and the more common) approach, but another approach is to use setsockopt()
to set the socket's SO_RCVTIMEO
option, which sets a timeout for blocking read operations (see SO_SENDTIMEO
for blocking sending operations). You can then call recvfrom()
and let it time out internally.
If you use the socket in non-blocking mode, then you can receive asynchronous FD_READ
notifications using WSAASyncSelect()
or WSAASyncEvent()
. No need to wait for timeouts.
If you use the socket in overlapped mode, then you can receive asynchronous read notifications from WSARecvFrom()
using WSAGetOverlappedResult()
or GetQueuedCompletionStatus()
. No need to wait for timeouts.