#include "vxWorks.h"
#include "stdio.h"
#include "inetLib.h"
#include "string.h"
#include "stdlib.h"
#include "sockLib.h"
#include "ioLib.h"
/*本地ip地址*/
#define LOCAL_IP_ADDR3 "192.168.1.10"
#define LOCAL_PORT_NUM3 5500
/*组播ip地址*/
#define REMOTE_IP_ADDR3 "224.2.2.2"
#define REMOTE_PORT_NUM3 5000
int tid10,tid11;
int sockFd3;
int sockAddrLen3;
struct sockaddr_in local3;
struct sockaddr_in remote3;
struct ip_mreq mcAddr;
struct sockaddr_in from3;
char sendBuf3[100]={"hello vxworks,I am WelBet..ZuBo!!!"},recvBuf3[100];
/********************************************************************************/
STATUS udp_zubo_init(void)
{
if((sockFd3=socket(AF_INET,SOCK_DGRAM,0))==ERROR) /*创建套接字*/
{
printf("OPEN SOCKET failed!!\n");
return ERROR;
}
logMsg("sockfd3 is: %d\n",sockFd3);
sockAddrLen3=sizeof(struct sockaddr_in);
local3.sin_family=AF_INET;
local3.sin_len=(u_char)sockAddrLen3;
local3.sin_port=htons(LOCAL_PORT_NUM3);
local3.sin_addr.s_addr=INADDR_ANY;
remote3.sin_family=AF_INET;
remote3.sin_len=(u_char)sockAddrLen3;
remote3.sin_port=htons(REMOTE_PORT_NUM3);
remote3.sin_addr.s_addr=inet_addr(REMOTE_IP_ADDR3);
if((bind(sockFd3,(struct sockaddr *)&local3,sockAddrLen3))==ERROR)
{
close(sockFd3);
printf("bind SOCKET failed!!\n");
return ERROR;
}
/******** 设置组播路由 非常重要 **********/
mRouteAdd(REMOTE_IP_ADDR3,LOCAL_IP_ADDR3,0xffffff00,0,0);
/**** 设定并加入多组播 *****/
mcAddr.imr_multiaddr.s_addr=inet_addr(REMOTE_IP_ADDR3);
mcAddr.imr_interface.s_addr=inet_addr(LOCAL_IP_ADDR3);
if(setsockopt(sockFd3,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&mcAddr,sizeof(struct ip_mreq))==ERROR)
{
close(sockFd3);
printf("add group failed\n");
return ERROR;
}
return OK;
}
/*******************************************************************************/
void task10(void)
{
while(1)
{
sendto(sockFd3,sendBuf3,50,0,(struct sockaddr *)&remote3,sockAddrLen3); /*组播发送数据*/
taskDelay(100);
}
}
void task11(void)
{
int fromLen4;
/*sockAddrLen4=sizeof(from3);*/
while(1)
{
logMsg("zubo \n");
recvfrom(sockFd3,recvBuf3,50,0,(struct sockaddr *)&from3,&fromLen4); /*组播接受数据*/
logMsg("recv data is:%s..time is:%d\n",recvBuf3,tickGet());
}
}
int main3(void)
{
udp_zubo_init();
tid10=taskSpawn("task10",100,0,1024,(FUNCPTR)task10,0,0,0,0,0,0,0,0,0,0);
tid11=taskSpawn("task11",105,0,1024,(FUNCPTR)task11,0,0,0,0,0,0,0,0,0,0);
return 1;
}