日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2024/4/21 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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;//組播相關結構體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?//查看路由表信息


具體操作過程如下圖:


總結

以上是生活随笔為你收集整理的【Linux网络编程】Linux多播问题(No such device)解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。