setsockopt调用IP_ADD_MEMBERSHIP出错errno:19 no such device

 

 if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {

 

                printf("setsockopt fail, try again ");
            usleep(200 * 1000);
                continue;

            }

提示: setsockopt fail, try again

此时,errno:19     --> no such device

这主要和当前的网络配置有关,因为多播IP地址没有加入到路由表中。

解决方法:把需要用到的多播地址(如本例的224.0.0.88)加入到路由表中,命令如下:

sudo route add -net 224.0.0.88 netmask 255.255.255.255 eth0

224.0.0.88:为当前使用的多播IP地址

eth0:为当前使用的有效网卡

其它辅助命令:

sudo route del -net 224.0.0.88 netmask 255.255.255.255 eth0 //把224.0.0.88从路由表中删除

route -n //查看路由表信息

 route add default gw 192.168.1.1 

原文地址:https://www.cnblogs.com/ordinary-world/p/10236861.html