嵌入式 Linux下进行广播包交互示例

2019-07-12 21:44发布

服务器端也就是接受广播包: #include #include #include #include #include #include #include /** 广播接收端代码 **/ #define IP_FOUND "IP_FOUND" #define IP_FOUND_ACK "IP_FOUND_ACK" #define PORT 9999 int main(int argc,char*argv[]){ int ret=-1; int sock; struct sockaddr_in server_addr;//服务器端地址 struct sockaddr_in from_addr;//客户端地址 int from_len=sizeof(struct sockaddr_in); int count=-1; fd_set readfd;//读文件描述符集合 char buffer[1024]; struct timeval timeout; timeout.tv_sec=2; timeout.tv_usec=0; sock=socket(AF_INET,SOCK_DGRAM,0);//建立数据报套接字 if(sock<0){ perror("sock error"); return; } memset((void*)&server_addr,0,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htons(INADDR_ANY); server_addr.sin_port=htons(PORT); //将地址结构绑定到套接字上./ ret=bind(sock,(struct sockaddr*)&server_addr,sizeof(server_addr)); if(ret<0){ perror("bind error"); return; } while(1){ printf("=============================Recv Broadcast=================================== "); timeout.tv_sec=2; timeout.tv_usec=0; //文件描述符集合清0 FD_ZERO(&readfd); //将套接字描述符加入到文件描述符集合 FD_SET(sock,&readfd); //select侦听是否有数据到来 ret=select(sock+1,&readfd,NULL,NULL,&timeout);//侦听是否可读 printf("ret=%d ",ret); switch(ret){ case -1://发生错误 break; case 0://超时 printf("timeout "); break; default: if(FD_ISSET(sock,&readfd)){ count=recvfrom(sock,buffer,1024,0,(struct sockaddr*)&from_addr,&from_len);//接收客户端发送的数据 //from_addr保存客户端的地址结构 if(strstr(buffer,IP_FOUND)){ //响应客户端请求 //打印客户端的IP地址 printf("Client IP is %s ",(char *)inet_ntoa(from_addr.sin_addr)); //打印客户端的端口号 printf("Client Send Port:%d ",ntohs(from_addr.sin_port)); memcpy(buffer,IP_FOUND_ACK,strlen(IP_FOUND_ACK)+1); count=sendto(sock,buffer,strlen(buffer),0,(struct sockaddr*)&from_addr,from_len);//将数据发送给客户端 } } break; } } return; }
客户端发送广播查找服务器: #include #include #include #include #include #include #include #include #include #include #include /** 客户端实现广播 **/ #define IP_FOUND "IP_FOUND" #define IP_FOUND_ACK "IP_FOUND_ACK" #define IFNAME "eth0" #define MCAST_PORT 9999 #define MAX_BROADCAST_NUM 16 int main(int argc,char*argv[]){ int ret=-1; struct sockaddr_in from_addr;//服务端地址 int from_len=sizeof(from_addr); int count=-1; fd_set readfd;//读文件描述符集合 char buffer[1024]; struct timeval timeout; timeout.tv_sec=2;//超时时间为2秒 timeout.tv_usec=0; int sock=-1; sock=socket(AF_INET,SOCK_DGRAM,0);//建立数据报套接字 if(sock<0){ printf("HandleIPFound:sock init error "); return; } //将使用的网络接口名字复制到ifr.ifr_name中,由于不同的网卡接口的广播地址是不一样的,因此指定网卡接口 struct ifreq ifr; bzero(&ifr, sizeof(ifr)); //strcpy(ifr.ifr_name, "eth0"); strncpy(ifr.ifr_name,IFNAME,strlen(IFNAME)); //发送命令,获得网络接口的广播地址 if(ioctl(sock,SIOCGIFBRDADDR,&ifr)==-1){ perror("ioctl error"); return; } //将获得的广播地址复制到broadcast_addr int so_broadcast=1; struct sockaddr_in broadcast_addr;//广播地址 memcpy(&broadcast_addr,&ifr.ifr_broadaddr,sizeof(struct sockaddr_in)); //设置广播端口号 printf("broadcast IP is:%s ",inet_ntoa(broadcast_addr.sin_addr)); broadcast_addr.sin_family=AF_INET; broadcast_addr.sin_port=htons(MCAST_PORT); //默认的套接字描述符sock是不支持广播,必须设置套接字描述符以支持广播 ret=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast)); while(1) { sleep(1); printf("=====================================Send Broadcast======================================== "); //发送多次广播,看网络上是否有服务器存在 int times = 1; int i=0; for(i=0;i