UDP Socket Set Timeout
I have the same problem. I tried to adopt the solution you suggested, using the timeval
struct. But it did not seem to work.
I have read on the Microsoft documentation and the time should be a DWORD
with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket
function, then the dwFlags
parameter must have the WSA_FLAG_OVERLAPPED
attribute set for the timeout to function properly.
Otherwise the timeout never takes effect.
The SO_RCVTIMEO
option expects a struct timeval
defined in sys/time.h
, not an integer like you're passing to it. The timeval struct
has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
perror("Error");
}