在openwrt路由器上运行的多播接收器软件不起作用

时间:2016-11-18 15:02:09

标签: c multicast openwrt

我编写了一个简单的多播接收程序,目前正在OpenWRT路由器中运行。但是该软件没有从组播组接收任何数据。

我在路由器中有一个工作的多播环境(我的无线设备可以从同一个多播组发送和接收数据),但如果我尝试使用我的软件从路由器收听该组,我什么也得不到。

注意:如果我在Linux桌面计算机上运行它,则相同的软件可以正常工作。

我在多播上做了tcpdump; 11.11.11.15和11.11.11.32是网络中两个已知的多播发送者:

root@OpenWrt:/# tcpdump -n multicast
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlan0, link-type EN10MB (Ethernet), capture size 65535 bytes
14:03:19.478476 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:24.480007 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:29.483429 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:34.479858 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:39.484171 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:44.483130 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:49.486375 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:54.492302 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:59.493509 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:04:04.495279 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:04:08.432849 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:08.432962 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:09.423884 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:09.424013 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26

多播接收器的源代码。

#define GROUP_PORT      12345
#define GROUP_ADDRESS   "239.0.0.38"

int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    struct ip_mreq mreq;
    int hndlSocket, nbytes,addrlen;
    char message[32];
    u_int allow=1;

    memset(&addr, 0, sizeof(addr));
    addr.sin_family         = AF_INET;
    addr.sin_addr.s_addr    = htonl(INADDR_ANY);
    addr.sin_port           = htons(GROUP_PORT);
    mreq.imr_multiaddr.s_addr   = inet_addr(GROUP_ADDRESS);
    mreq.imr_interface.s_addr   = htonl(INADDR_ANY);

    if ((hndlSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
    {
        perror("socket");
    }
    else if (setsockopt(hndlSocket, SOL_SOCKET, SO_REUSEADDR, &allow, sizeof(allow)) < 0) 
    {
        perror("Reusing ADDR failed");
    }
    else if (bind(hndlSocket, (struct sockaddr *) &addr, sizeof(addr)) < 0) 
    {
        perror("bind");
    }
    else if (setsockopt(hndlSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) 
    {
        perror("setsockopt for multi membership");
    }
    else
    {
        /* now just enter a read-print loop */
        while (true)
        {
            addrlen = sizeof(addr);
            if ((nbytes=recvfrom(hndlSocket, message, sizeof(message), 0, (struct sockaddr *) &addr, &addrlen)) < 0) 
            {
                perror("recvfrom"); exit(1);
            }
            puts(message);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

添加以下路线后,它就像一个魅力。

route add -net 224.0.0.0 netmask 224.0.0.0 wlan0