简体   繁体   中英

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 xxx255 host, and all my apps recives it, but im wondering from where that errno came (errno=2=no such file or directory)

best regards

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM