加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:47:29 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 //?发送端??#include?iostream??#include?stdio.h??#include?sys/socket.h??#include?unistd.h??#include?sys/types.h??#include?netdb.h??#include?n

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

//?发送端??
#include?<iostream>??
#include?<stdio.h>??
#include?<sys/socket.h>??
#include?<unistd.h>??
#include?<sys/types.h>??
#include?<netdb.h>??
#include?<netinet/in.h>??
#include?<arpa/inet.h>??
#include?<string.h>??


using?namespace?std;??

int?main()??
{??
????setvbuf(stdout,?NULL,?_IONBF,?0);???
????fflush(stdout);???

????int?sock?=?-1;??
????if?((sock?=?socket(AF_INET,?SOCK_DGRAM,?0))?==?-1)???
????{?????
????????cout<<"socket?error"<<endl;???
????????return?false;??
????}?????

????const?int?opt?=?1;??
????//设置该套接字为广播类型,??
????int?nb?=?0;??
????nb?=?setsockopt(sock,?SOL_SOCKET,?SO_BROADCAST,?(char?*)&opt,?sizeof(opt));??
????if(nb?==?-1)??
????{??
????????cout<<"set?socket?error..."<<endl;??
????????return?false;??
????}??

????struct?sockaddr_in?addrto;??
????bzero(&addrto,?sizeof(struct?sockaddr_in));??
????addrto.sin_family=AF_INET;??
????addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST);??
????addrto.sin_port=htons(6000);??
????int?nlen=sizeof(addrto);??

????while(1)??
????{??
????????sleep(1);??
????????//从广播地址发送消息??
????????char?smsg[]?=?{"abcdef"};??
????????int?ret=sendto(sock,?smsg,?strlen(smsg),?0,?(sockaddr*)&addrto,?nlen);??
????????if(ret<0)??
????????{??
????????????cout<<"send?error...."<<ret<<endl;??
????????}??
????????else??
????????{?????????
????????????printf("ok?");????
????????}??
????}??

????return?0;??
}
//?接收端

#include?<iostream>??
#include?<stdio.h>??
#include?<sys/socket.h>??
#include?<unistd.h>??
#include?<sys/types.h>??
#include?<netdb.h>??
#include?<netinet/in.h>??
#include?<arpa/inet.h>??
#include?<string.h>??


using?namespace?std;??

int?main()??
{??
????setvbuf(stdout,?0);???
????fflush(stdout);???

????//?绑定地址??
????struct?sockaddr_in?addrto;??
????bzero(&addrto,?sizeof(struct?sockaddr_in));??
????addrto.sin_family?=?AF_INET;??
????addrto.sin_addr.s_addr?=?htonl(INADDR_ANY);??
????addrto.sin_port?=?htons(6000);??

????//?广播地址??
????struct?sockaddr_in?from;??
????bzero(&from,?sizeof(struct?sockaddr_in));??
????from.sin_family?=?AF_INET;??
????from.sin_addr.s_addr?=?htonl(INADDR_ANY);??
????from.sin_port?=?htons(6000);??

????int?sock?=?-1;??
????if?((sock?=?socket(AF_INET,?sizeof(opt));??
????if(nb?==?-1)??
????{??
????????cout<<"set?socket?error..."<<endl;??
????????return?false;??
????}??

????if(bind(sock,(struct?sockaddr?*)&(addrto),?sizeof(struct?sockaddr_in))?==?-1)???
????{?????
????????cout<<"bind?error..."<<endl;??
????????return?false;??
????}??

????int?len?=?sizeof(sockaddr_in);??
????char?smsg[100]?=?{0};??

????while(1)??
????{??
????????//从广播地址接受消息??
????????int?ret=recvfrom(sock,?100,?(struct?sockaddr*)&from,(socklen_t*)&len);??
????????if(ret<=0)??
????????{??
????????????cout<<"read?error...."<<sock<<endl;??
????????}??
????????else??
????????{?????????
????????????printf("%st",?smsg);?????
????????}??

????????sleep(1);??
????}??

????return?0;??
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读