Linux multicast problem (No such device) workaround

Source: Internet
Author: User



The test code for multicasting is as follows:




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

int main(int argc, char*argv[])
{
int sockfd; // Socket file descriptor
struct sockaddr_in local_addr; // local address
int err = -1;
char group[16] = "224.0.0.88"; // Multicast group IP
To
sockfd = socket(AF_INET, SOCK_DGRAM, 0); //Create a socket
if (sockfd == -1)
{
perror("socket()");
return -1;
}
To
// Initialize address
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(8080);
To
// bind socket
err = bind(sockfd,(struct sockaddr*)&local_addr, sizeof(local_addr));
if(err <0)
{
perror("bind()");
return -2;
}

struct ip_mreq mreq; // Multicast address structure

// Joining a multicast group is equivalent to creating a QQ group, and someone joins this group
mreq.imr_multiaddr.s_addr = inet_addr(group); // Multicast address, similar to QQ group number
mreq.imr_interface.s_addr = htonl(INADDR_ANY);// Add this machine to the multicast group, similar to someone joining this group
To
// Join the multicast group
err = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
if (err <0)
{
perror("[50]setsockopt():IP_ADD_MEMBERSHIP");
return -4;
}

int times = 0;
int addr_len = 0;
char buff[256] = {0};
int n = 0;
To
printf("Successfully joined the multicast group, ready to receive data\n");  //Receive data
while(1)
{
addr_len = sizeof(local_addr);
memset(buff, 0, sizeof(buff));
To  // Receive data 
n = recvfrom(sockfd, buff, sizeof(buff), 0,(struct sockaddr*)&local_addr, &addr_len);
if( n == -1)
{
perror("recvfrom()");
close(sockfd);
return -1;
}

printf("Recv message from server: %s\n", buff);
}
To
// Exit the broadcast group
err = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP,&mreq, sizeof(mreq));
if (err <0)
{
perror("[79]setsockopt():IP_DROP_MEMBERSHIP");
return -4;
}

To
close(sockfd);

return 0;
} 

When ubuntu compiles and runs, the following error appears:


The answers to inquiries about relevant information are as follows:

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.


This is mainly related to the current network configuration, because the multicast IP address is not added to the routing table.


Solution: Add the required multicast address (such as 224.0.0.88 in this example) to the routing table, the command is as follows:

sudo route add -net 224.0.0.88 netmask 255.255.255.255 eth0

224.0.0.88: is the current multicast IP address

eth0: is the valid network card currently in use


Other auxiliary commands:

sudo route del -net 224.0.0.88 netmask 255.255.255.255 eth0 //Delete 224.0.0.88 from the routing table

route -n //View routing table information



To download the test code of this tutorial, please click this link: http://download.csdn.net/detail/tennysonsky

Copyright notice: Most of this blog post is compiled by myself, or collected on the Internet, please indicate the source for reprinting! !

Linux multicast problem (No such device) solution




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.