void udpclient(int argc, char **argv) { int myFd, clientFd; struct sockaddr_in myAddr, clientAddr; struct ip_mreq mreq; int addrlen = sizeof(clientAddr); char buf[MAXBUF]; char addrTemp[INET_ADDRSTRLEN]; int db_Value=0; int i,fd_pipe_w; rt_memset(&myAddr, 0,sizeof(myAddr)); rt_memset(&clientAddr, 0,sizeof(clientAddr)); //创建套接字,datagrams if((myFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { } //设置本地地址端口信息 myAddr.sin_family = AF_INET; myAddr.sin_addr.s_addr = INADDR_ANY; if (argc == 3) { myAddr.sin_port = htons(atoi(argv[2])); } else { myAddr.sin_port = htons(PORT); } //绑定本地套接字 if(bind(myFd, (struct sockaddr *)&myAddr, sizeof(myAddr)) != 0) { } //设置IP_MULTICAST_LOOP unsigned char loop = 1; if (setsockopt(myFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) != 0) { return ; } //设置IP_MULTICAST_TTL 为1,即该多播最多只能经过一个路由器 unsigned char ttl = 1; if (setsockopt(myFd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) != 0) { return ; } //对struct ip_mreq这个结构体赋值 if (argc > 1) { //设置IP地址 int res; if ((res = inet_pton(AF_INET, argv[1], &mreq.imr_multiaddr)) == -1) { exit(errno); } else if(res == 0) { rt_kprintf("IP地址无效"); rt_kprintf("请输入一个多播地址:224.0.0.1 ~ 239.255.255.255"); return ; } } else { rt_kprintf("Usage:%s 多播地址(224.0.0.1 ~ 239.255.255.255)", argv[0]); return ; } mreq.imr_interface.s_addr = INADDR_ANY; //将本机加入多播组中 if (setsockopt (myFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) { exit(errno); } rt_kprintf("Multicast is starting,waitting for information of Multicast....."); while (1) { rt_memset(buf, 0,sizeof(buf)); rt_kprintf("wait UDP data "); if (recvfrom(myFd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&clientAddr, &addrlen) == -1) { return ; } rt_kprintf("....... "); /* 线程休眠一段时间 */ rt_thread_delay(50); } //退出广播组 if (setsockopt (myFd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) { exit(errno); } return; } ``` [table=98%] [tr][td=760][align=center]1、硬件:正点原子阿波罗F429 2、想实现接受来自239.255.255.1 的多播数据,程序在linux可用,但是移植到到RT-Thread调试了很久都没有结果,希望有大兄弟能指点一下迷津。如果有大兄弟有RT-Thread的多播接收代码,有偿使用。[/td][/tr] [/table]
查看更多
回到 顶部
发布 问题
分享 好友
手机 浏览
投诉 建议
回到 底部