This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

EPERM error on setsockopt() on UDP socket

Hi,, I'm trying to set a recv timeout on a UDP socket (coap) using setsockopt() and getting an EPERM error. I've tried the setsockopt() call both before and after the (successful) connect(), with the same result. Am I doing this right? Or is there a problem with timeout support on UDP sockets? I've looked at this post: https://devzone.nordicsemi.com/f/nordic-q-a/52437/nrf_poll-may-be-consuming-data-when-not-supposed-to-on-nrf9160, and can't see a difference in my usage, other than the different socket type.

Code snippet:

------------------------------

for (addr = result; addr != NULL; addr = addr->ai_next) {

sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
err = sock;
continue;
}

err = connect(sock, addr->ai_addr, addr->ai_addrlen);
if (err < 0) {
continue;
}

// Set the timeout here for responses.
struct timeval tv;
// tv.tv_sec = COAP_RESPONSE_TIMEOUT_MS / 1000;
tv.tv_sec = 1;
// tv.tv_usec = (COAP_RESPONSE_TIMEOUT_MS % 1000) * 1000;
tv.tv_usec = 0;
err = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof(tv));
if (err < 0) {
LOG_ERR("setsockopt() failed, err = %d.", err);
} else {
LOG_DBG("setsockopt() succeeded.");
}

// If connect() succeeded, use this address.
break;
}

--------------------

Thanks,

Chris.