vxworks UDP 组播发送
各位大哥帮忙看看我的组播发送函数,sendto返回老是ERROR,发送失败,是什么原因?谢谢。
struct sockaddr_in local;
struct sockaddr_in target;
struct ip_mreq ipMreq;
int localfd;
int i,recvbytes,sendbytes;
char message[REQUEST_MSG_SIZE];
struct ip_mreq mcast;
client_tx=0;
client_tx_err=0;
bzero((char *)&local,sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr("202.195.114.224");
local.sin_port = htons(SERVER_PORT_NUM);
if((localfd = socket( AF_INET, SOCK_DGRAM, 0)) == ERROR)
{
printf("frame send Task socket() error:%s\n",strerror(errno));
return ERROR;
}
if(bind(localfd, (struct sockaddr *)&local, sizeof(struct sockaddr_in))==ERROR)
{
printf("frame send Task bind() error:%s\n",strerror(errno));
return ERROR;
}
ipMreq.imr_multiaddr.s_addr = inet_addr("224.1.0.1");
ipMreq.imr_interface.s_addr = inet_addr("202.195.114.224");
if(setsockopt(localfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq, sizeof(ipMreq)) == ERROR)
{
printf("setsockopt error, %s.\n", strerror(errno));
}
else
{
printf("join group ok.\n");
}
bzero((char *)&target,sizeof(target));
target.sin_family = AF_INET;
target.sin_addr.s_addr = inet_addr("224.1.0.1");
target.sin_port = htons(5002);
memset(message, 'Z', REQUEST_MSG_SIZE);
FOREVER
{
/*memset(message, client_tx & 0xff, REQUEST_MSG_SIZE);*/
memset(message, 'Z', REQUEST_MSG_SIZE);
delays(1250000 / 3);
/*taskDelay(1);*/
if((sendbytes=sendto (localfd, (char *) message, length, 0,(struct sockaddr *) &target,
sizeof(struct sockaddr_in)) == ERROR))
{
printf("mcastSend sendto errno:0x%x\n", strerror(errno));
break;
}
if (sendbytes != length)
{
client_tx_err++;
break;
}
else
client_tx++;
if(client_tx==count)
{
printf("count value is %d\n", count);
break;
}
}
close (localfd);
return OK;
[解决办法]
在
if(setsockopt(localfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq, sizeof(ipMreq)) == ERROR)
前加上
routeAdd("224.1.0.1","202.195.114.224");
试试
[解决办法]
加上组播路由即可。
routeAdd