I want to send a UDP packet to a camera from the PC when the PC resumes from sleep. Since it takes some time (unknown) to the network interface to become alive after the PC resumes, I keep sending packets to the camera in a loop. When the camera receives the packet, it sends an acknowledge signal to the PC.
My problem is “for receiving the UDP packet from the camera (ack signal), I use recvfrm() function which blocks the loop. How do I unblock this function so that it exit the loop only when it receives acknowledge signal from the camera.
I want to send a UDP packet to a camera from the PC when
Share
A more portable solution to maverik’s answer (which is otherwise correct) would be to
fcntlthe socket toO_NONBLOCK.MSG_DONTWAIT, although available under Linux and BSD and most Unices is only standardized in SUSv4 for sending (why, I wouldn’t know… but M. Kerrisk says so). One notable platform which doesn’t support it is Winsock (at least it’s not documented in MSDN).Alternatively, if you don’t want to tamper with obscure flags and
fcntl, you couldselectthe descriptor for readiness (with a zero timeout, or even with a non-zero timeout to throttle the packets you send — it’s probably a good idea not to flood the network stack). Keep sending untilselectsays something can be read.