码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2014-10-11 19:11:56      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   strong   sp   

  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. }  

 

  1. // 接收端 
  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系统下UDP发送和接收广播消息小样例

标签:style   blog   http   color   io   os   ar   strong   sp   

原文地址:http://www.cnblogs.com/lcchuguo/p/4019476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!