Linux系统下UDP发送和接收广播消息小例子

简介: [cpp] view plaincopy   // 发送端   #include    #include    #include    #include    #include    #include    #include    #include    #in...
[cpp]  view plain copy
 
  1. // 发送端  
  2. #include <iostream>  
  3. #include <stdio.h>  
  4. #include <sys/socket.h>  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <netdb.h>  
  8. #include <netinet/in.h>  
  9. #include <arpa/inet.h>  
  10. #include <string.h>  
  11.   
  12.   
  13. using namespace std;  
  14.   
  15. int main()  
  16. {  
  17.     setvbuf(stdout, NULL, _IONBF, 0);   
  18.     fflush(stdout);   
  19.   
  20.     int sock = -1;  
  21.     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)   
  22.     {     
  23.         cout<<"socket error"<<endl;   
  24.         return false;  
  25.     }     
  26.       
  27.     const int opt = 1;  
  28.     //设置该套接字为广播类型,  
  29.     int nb = 0;  
  30.     nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));  
  31.     if(nb == -1)  
  32.     {  
  33.         cout<<"set socket error..."<<endl;  
  34.         return false;  
  35.     }  
  36.   
  37.     struct sockaddr_in addrto;  
  38.     bzero(&addrto, sizeof(struct sockaddr_in));  
  39.     addrto.sin_family=AF_INET;  
  40.     addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST);  
  41.     addrto.sin_port=htons(6000);  
  42.     int nlen=sizeof(addrto);  
  43.   
  44.     while(1)  
  45.     {  
  46.         sleep(1);  
  47.         //从广播地址发送消息  
  48.         char smsg[] = {"abcdef"};  
  49.         int ret=sendto(sock, smsg, strlen(smsg), 0, (sockaddr*)&addrto, nlen);  
  50.         if(ret<0)  
  51.         {  
  52.             cout<<"send error...."<<ret<<endl;  
  53.         }  
  54.         else  
  55.         {         
  56.             printf("ok ");    
  57.         }  
  58.     }  
  59.   
  60.     return 0;  
  61. }  

 

[cpp]  view plain copy
 
  1. // 接收端 http://blog.csdn.net/robertkun  
  2.   
  3. #include <iostream>  
  4. #include <stdio.h>  
  5. #include <sys/socket.h>  
  6. #include <unistd.h>  
  7. #include <sys/types.h>  
  8. #include <netdb.h>  
  9. #include <netinet/in.h>  
  10. #include <arpa/inet.h>  
  11. #include <string.h>  
  12.   
  13.   
  14. using namespace std;  
  15.   
  16. int main()  
  17. {  
  18.     setvbuf(stdout, NULL, _IONBF, 0);   
  19.     fflush(stdout);   
  20.   
  21.     // 绑定地址  
  22.     struct sockaddr_in addrto;  
  23.     bzero(&addrto, sizeof(struct sockaddr_in));  
  24.     addrto.sin_family = AF_INET;  
  25.     addrto.sin_addr.s_addr = htonl(INADDR_ANY);  
  26.     addrto.sin_port = htons(6000);  
  27.       
  28.     // 广播地址  
  29.     struct sockaddr_in from;  
  30.     bzero(&from, sizeof(struct sockaddr_in));  
  31.     from.sin_family = AF_INET;  
  32.     from.sin_addr.s_addr = htonl(INADDR_ANY);  
  33.     from.sin_port = htons(6000);  
  34.       
  35.     int sock = -1;  
  36.     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)   
  37.     {     
  38.         cout<<"socket error"<<endl;   
  39.         return false;  
  40.     }     
  41.   
  42.     const int opt = 1;  
  43.     //设置该套接字为广播类型,  
  44.     int nb = 0;  
  45.     nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));  
  46.     if(nb == -1)  
  47.     {  
  48.         cout<<"set socket error..."<<endl;  
  49.         return false;  
  50.     }  
  51.   
  52.     if(bind(sock,(struct sockaddr *)&(addrto), sizeof(struct sockaddr_in)) == -1)   
  53.     {     
  54.         cout<<"bind error..."<<endl;  
  55.         return false;  
  56.     }  
  57.   
  58.     int len = sizeof(sockaddr_in);  
  59.     char smsg[100] = {0};  
  60.   
  61.     while(1)  
  62.     {  
  63.         //从广播地址接受消息  
  64.         int ret=recvfrom(sock, smsg, 100, 0, (struct sockaddr*)&from,(socklen_t*)&len);  
  65.         if(ret<=0)  
  66.         {  
  67.             cout<<"read error...."<<sock<<endl;  
  68.         }  
  69.         else  
  70.         {         
  71.             printf("%s\t", smsg);     
  72.         }  
  73.   
  74.         sleep(1);  
  75.     }  
  76.   
  77.     return 0;  
  78. }  


自已在Linux虚拟机下测试可以成功, 前提是要把主机设置在同一网段内, 还有就是不要忘记关闭Linux的防火墙.. 可以使用setup命令进行设置。

(我在测试的时候只能发送不收接收,折磨了我半天,后来才想到是Linux防火墙的问题。。)

 

关于虚拟机的网卡配置,建议选择桥接模式。NAT的模式的话,是受限制的,可能会收不到广播消息。

具体的参考网上的文章吧。。

祝你成功。。

目录
相关文章
|
4天前
|
Ubuntu 安全 Linux
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
36 1
|
11天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
52 2
|
13天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
71 0
|
14天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
28 0
|
21天前
|
存储 前端开发 Linux
Linux系统之部署ToDoList任务管理工具
【4月更文挑战第1天】Linux系统之部署ToDoList任务管理工具
63 1
|
22天前
|
存储 传感器 运维
linux系统资源统计工具
【4月更文挑战第1天】Linux系统监控工具如dstat、htop、glances、vmstat、top、iostat、mpstat、sar和atop,用于跟踪CPU、内存、磁盘I/O、网络和进程性能。这些工具提供实时、交互式和历史数据分析,助力管理员优化系统性能和故障排查。例如,dstat是vmstat等工具的增强版,htop提供彩色界面的进程管理,而atop则结合了多种功能并记录历史数据。
28 5
linux系统资源统计工具
|
12天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
32 6
|
22天前
|
Ubuntu 架构师 Java
Linux系统常用命令非常详细建议收藏
Linux系统常用命令非常详细建议收藏
49 0
|
1天前
|
资源调度 JavaScript Ubuntu
Linux系统之部署briefing视频聊天系统
【4月更文挑战第21天】Linux系统之部署briefing视频聊天系统
9 1
|
2天前
|
Linux Perl
Linux系统替换字符串常用命令
请注意,`sed`命令可以非常强大,可以根据不同的需求使用不同的选项和正则表达式来进行更复杂的字符串替换操作。
16 0