【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 128int main(void) {int ret = -1;int sockfd = -1;int i = 0;char buf[SIZE];struct sockaddr_in addr;struct sockaddr_in from;//組播相關(guān)結(jié)構(gòu)體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++;//退出循環(huán)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++;//退出循環(huán)if (10 == i)break;}close(sockfd);return 0; err1:close(sockfd); err0:return -1; }
在ubuntu編譯運行時,出現(xiàn)如下的錯誤:
查詢相關(guān)資料得到的答案如下:
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.
這主要和當(dāng)前的網(wǎng)絡(luò)配置有關(guān),因為多播IP地址沒有加入到路由表中。
解決方法:把需要用到的多播地址(如本例的224.0.0.88)加入到路由表中,命令如下:
sudo route add -net?224.0.0.88?netmask 255.255.255.255 eth0
224.0.0.88:為當(dāng)前使用的多播IP地址
eth0:為當(dāng)前使用的有效網(wǎng)卡
其它輔助命令:
sudo route del -net 224.0.0.88 netmask 255.255.255.255 eth0?//把224.0.0.88從路由表中刪除
route -n?//查看路由表信息
具體操作過程如下圖:
總結(jié)
以上是生活随笔為你收集整理的【Linux网络编程】Linux多播问题(No such device)解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux网络编程】多播、组播
- 下一篇: 【Linux网络编程】TCP