JosiP
JosiP

Reputation: 3079

setsockopt returns errno=2

Im creating an UDP socket (centos 6), which i want to send broadcast message. Everything works, creating socket works - socket(..) returns value=25, but:

int val = 1;
      if (setsockopt(a, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) < 1){
          debug("setsockoopt failed with errno: %d, socket %d", errno, a);
      }

setsockopt sets errno to value = 2. Communictaion works, im sending udp packet to x.x.x.255 host, and all my apps recives it, but im wondering from where that errno came (errno=2=no such file or directory)

best regards

Upvotes: 1

Views: 3403

Answers (1)

fvu
fvu

Reputation: 32953

Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

That's from the setsockopt manpage returns 0 on success, so it's just your condition that's wrong. If there was no error, errno's value is not relevant, and that's why here you get a rather absurd value.

if (setsockopt(a, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) != 0){

is what you need.

Upvotes: 5

Related Questions