mode: 命令 msg: 附加信息 struct sockaddr *p:网络信息 fd:网络套接字描述符
int msg_send(const int mode,const char *msg,const struct sockaddr *p,int fd) { int udp_fd=fd; int broadcast_en=1; char msg_buf[SND_BUF_LEN]; char *use="test",*group="sunplusapp"; socklen_t broadcast_len=sizeof(broadcast_en); long int msg_id=time((time_t *)NULL); struct sockaddr_in udp_addr; struct sockaddr client; bzero(msg_buf,SND_BUF_LEN); bzero(&udp_addr,sizeof(struct sockaddr_in)); udp_addr.sin_family=AF_INET; udp_addr.sin_port=htons(IPMSG_UDP_PORT); inet_pton(AF_INET,BR_IP,&udp_addr.sin_addr.s_addr);
//下边的if 与else if :对于上线通告 下线等使用广播地址,其他的则否 if( (p==NULL)&&(mode!=IPMSG_NOOPERATION)&&(mode!=IPMSG_BR_ENTRY)&&(mode!=IPMSG_BR_EXIT)) { printf("p is NULL,only mode = IPMSG_NOOPERATICNA IPMSG_BR_ENTRY IPMSG_EXIT is allowed p=NULL \n"); return 1; } else if( (p!=NULL)&&(mode!=IPMSG_NOOPERATION)&&(mode!=IPMSG_BR_ENTRY)&&(mode!=IPMSG_BR_EXIT)) client=*p;
//打开广播 if( setsockopt(udp_fd,SOL_SOCKET,SO_BROADCAST,&broadcast_en,broadcast_len)<0 ) { perror("setsockopt error"); exit(1); } switch (mode) { case IPMSG_NOOPERATION: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,NULL); sendto(udp_fd,msg_buf,strlen(msg_buf),0,(struct sockaddr *)&udp_addr,sizeof(struct sockaddr)); break; case IPMSG_BR_ENTRY: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,(struct sockaddr *)&udp_addr,sizeof(struct sockaddr)); break; case IPMSG_BR_EXIT: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,(struct sockaddr *)&udp_addr,sizeof(struct sockaddr)); break; case IPMSG_ANSENTRY: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_SENDMSG: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_SENDMSG_OPT: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_RECVMSG: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_GETFILEDATA: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_RELEASEFILES: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; case IPMSG_GETDIRFILES: sprintf(msg_buf,"1:%d:%s:%s:%d:%s",msg_id,use,group,mode,msg); sendto(udp_fd,msg_buf,strlen(msg_buf),0,&client,sizeof(struct sockaddr)); break; default: printf("no match mode !\n"); break; } broadcast_en=0;
// 关掉广播 if( setsockopt(udp_fd,SOL_SOCKET,SO_BROADCAST,&broadcast_en,broadcast_len)<0 ) { perror("setsockopt error"); exit(1); } printf("msg send ok ! \n"); return 0; }
|