【Linux网络编程】Linux多播问题(No such device)解决方法

多播的测试代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 10086
#define SIZE 128


int main(void)
{
    int ret = -1;
    int sockfd = -1;
    int i = 0;
    char buf[SIZE];
    struct sockaddr_in addr;
    struct sockaddr_in from;
    //组播相关结构体
    struct ip_mreq req;
    socklen_t len = sizeof(from);

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sockfd)
    {
        perror("socket"); 
        goto err0;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = INADDR_ANY;
    //inet_pton(AF_INET, "172.16.1.88", &addr.sin_addr); 

    ret = bind(sockfd, (void*)&addr, sizeof(addr));
    if (-1 == ret)
    {
        perror("bind"); 
        goto err1;
    }

    printf("UDP Server %s: %d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));

    //加入多播组
    req.imr_multiaddr.s_addr = inet_addr("224.0.0.88");
    //将本机加入多播组
    req.imr_interface.s_addr = INADDR_ANY;

    //加入多播组
    ret = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req));
    if (ret < 0)
    {
        perror("setsockopt"); 
        goto err0;
    }

    while(1)
    {
        memset(buf, 0, SIZE);

        ret = recvfrom(sockfd, buf, SIZE, 0, (void*)&from, &len);
        buf[ret] = 0;

        printf("recv from: %s:%d  %s\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port), buf);
        i++;
        //退出循环
        if (10 == i)
            break;
    }

    ret = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &req, sizeof(req));
    if (ret < 0)
    {
        perror("setsockopt"); 
        goto err0;
    }
        
    //退出组播组之后 测试是否可以收到组播包
    while(1)
    {
        memset(buf, 0, SIZE);

        ret = recvfrom(sockfd, buf, SIZE, 0, (void*)&from, &len);
        buf[ret] = 0;

        printf("recv from: %s:%d  %s\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port), buf);
        i++;
        //退出循环
        if (10 == i)
            break;
    }


    

    close(sockfd);
    return 0;
err1:
    close(sockfd);
err0:
    return -1;
}


在ubuntu编译运行时,出现如下的错误:



查询相关资料得到的答案如下:

It means that the tool is trying to use multicast but the network interface doesn't support it There are two likely causes:
·Your machine doesn't have multicast support enabled. For example, on Linux and FreeBSD it is possible to compile a kernel which doesn't support multicast.  
·You don't have a route for multicast traffic. Some systems don't add this by default, and you need to run:
route add -net 224.0.0.0 netmask 255.255.255.255 eth0(or similar). If you wish to use RAT in unicast mode only, it is possible to add the multicast route on the loopback interface.


这主要和当前的网络配置有关,因为多播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 //查看路由表信息


具体操作过程如下图:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值