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的模式的话,是受限制的,可能会收不到广播消息。

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

祝你成功。。

目录
相关文章
|
7天前
|
Ubuntu 安全 Linux
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
40 1
|
14天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
53 2
|
17天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
90 0
|
17天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
31 0
|
15天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
39 6
|
1天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
12 1
|
2天前
|
Linux 数据安全/隐私保护
Linux系统忘记密码的三种解决办法
这篇博客介绍了三种在Linux忘记密码时重置登录密码的方法:1) 使用恢复模式,通过控制台界面以管理员权限更改密码;2) 利用Linux Live CD/USB启动,挂载硬盘分区并使用终端更改密码;3) 进入单用户模式,自动以管理员身份登录后重置密码。每个方法都提供了详细步骤,提醒用户在操作前备份重要数据。
|
2天前
|
JSON Unix Linux
Linux系统之jq工具的基本使用
Linux系统之jq工具的基本使用
29 2
|
2天前
|
数据采集 监控 安全
linux系统被×××后处理经历
linux系统被×××后处理经历
|
3天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
41 2