完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
如题,在vxworks上实现udp广播,然而没有开发板,用Tornado中的vxsim模拟,代码如下
#include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "hostLib.h" #include "ioLib.h" #include "strLib.h" #define SA_LEN sizeof(struct sockaddr_in) #define MSG "1234567890" #define MSG_SIZE 11 /*broadcast function*/ STATUS bcast (void){ int bcastSocket;/*广播socket*/ struct sockaddr_in bcastAddr;/*广播地址*/ int bcastFlag = 1;/*允许广播设置为1*/ /*要为广播socket绑定本地端口地址*/ struct sockaddr_in nativeAddr; bzero((char*) &nativeAddr,SA_LEN); nativeAddr.sin_family = AF_INET; nativeAddr.sin_len = SA_LEN; nativeAddr.sin_port = htons(5000); if((nativeAddr.sin_addr.s_addr = hostGetByName("vxTarget")) == ERROR) { perror("Client:unknown server name.n"); return(ERROR); } /*创建socket*/ if((bcastSocket = socket(AF_INET,SOCK_DGRAM,0)) < 0 ){ perror("bcast: socket"); close(bcastSocket); return ERROR; } /*绑定本地端口地址*/ if(bind(bcastSocket,(struct sockaddr *) &nativeAddr,SA_LEN) == ERROR) { perror("Server:bind failed.n"); close(bcastSocket); return(ERROR); } /*设置socket为可以发送广播的*/ if(setsockopt(bcastSocket,SOL_SOCKET,SO_BROADCAST,(char*)&bcastFlag,sizeof(bcastFlag)) == -1 ){ perror("bcast:setsockopt"); close(bcastSocket); return ERROR; } /*初始化广播地址*/ bzero((char*) &bcastAddr,SA_LEN); bcastAddr.sin_family = AF_INET; bcastAddr.sin_len = SA_LEN; bcastAddr.sin_port = htons(5000); bcastAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); /*发送广播消息*/ if(sendto (bcastSocket,MSG,MSG_SIZE,0,(struct sockaddr *)&bcastAddr,SA_LEN) < 0) perror("bcast broadcast"); close(bcastSocket); return(OK); } /*recvive function*/ STATUS jieshou() { int s; struct sockaddr_in clientAddr,serverAddr; int addrLen = SA_LEN; char recvBuf[MSG_SIZE+1]; int nRecv; int set = 1; s = socket(AF_INET,SOCK_DGRAM,0); if( s < 0 ) { perror("Server: opening datagram socket"); return(ERROR); } bzero((char *) &serverAddr,SA_LEN); serverAddr.sin_family = AF_INET; serverAddr.sin_len = SA_LEN; serverAddr.sin_port = htons(5000); serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(s,(struct sockaddr *) &serverAddr,SA_LEN) == ERROR) { perror("Server:bind failed.n"); close(s); return(ERROR); } while(TRUE) { printf("nRecv is: %dn",nRecv); nRecv = recvfrom(s, recvBuf,MSG_SIZE,0,(struct sockaddr *) &clientAddr,&addrLen); printf("nRecv is: %dn",nRecv); if(nRecv < 0) { perror("Server:recvfrom"); continue; } printf("msg = %sn",recvBuf); printf("node[%s]:the connected client ip=[%s],port= [%hd]n",inet_ntoa(serverAddr.sin_addr),inet_ntoa(clientAddr.sin_addr),clientAddr.sin_port); } } 以上代码可通过编译而且执行时不会报错,广播函数sendto发送正常,但是接收函数无法接收到广播消息。请问各位大神这是怎么回事?难道是Tornado2.2中的vxsim不支持广播?另外,我已经在这个环境下用两个vxsim实现了udp的单播。 |
|
相关推荐
1个回答
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
761 浏览 1 评论
请问Vxworks remove 返回值 错误码 errno为0xb83dd0 表示什么意思?
2302 浏览 0 评论
4248 浏览 1 评论
5086 浏览 0 评论
采用普通裸机boot跳转到VxWorks_rom镜像启动可行吗
3410 浏览 0 评论
763浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-12 23:37 , Processed in 0.558882 second(s), Total 82, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号