在Tms320C6678开发板上做测试,使用mcsdk中的helloworld作为原型,使用NDK安装目录下的一个MulticastTest() 函数稍作修改,进行多播接收测试,
代码如下所示:
/*---------------------------------------------------------------------- */
/* MulticastTest() */
/* Test the Multicast socket API. */
/*---------------------------------------------------------------------- */
void dtask_udp_multicast (void)
{
SOCKET sudp1 = INVALID_SOCKET;
struct sockaddr_in sin1;
char buffer[1000];
int reuse = 1;
struct ip_mreq group;
fd_set msockets;
int iterations = 0;
int cnt;
CI_IPNET NA;
/* Raise priority to transfer data & wait for the link to come up. */
TaskSetPri(TaskSelf(), 1);
/* Allocate the file environment for this task */
fdOpenSession( TaskSelf() );
printf ("=== Executing Multicast Test on Interface 1 ===\n");
/* Create our UDP Multicast socket1 */
sudp1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( sudp1 == INVALID_SOCKET )
{
printf ("Error: Unable to create socket\n");
return;
}
/* Set Port = 4040, leaving IP address = Any */
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_port = htons(7003);
/* Print the IP address information only if one is present. */
if (CfgGetImmediate( 0, CFGTAG_IPNET, 1, 1, sizeof(NA), (UINT8 *)&NA) != sizeof(NA))
{
printf ("Error: Unable to get IP Address Information\n");
fdClose (sudp1);
return;
}
/* Set the Reuse Ports Socket Option for both the sockets. */
if (setsockopt(sudp1, SOL_SOCKET, SO_REUSEPORT, (char *)&reuse, sizeof(reuse)) < 0)
{
printf ("Error: Unable to set the reuse port socket option\n");
fdClose (sudp1);
return;
}
/* Now bind both the sockets. */
if (bind (sudp1, (PSA) &sin1, sizeof(sin1)) < 0)
{
printf ("Error: Unable to bind the socket.\n");
fdClose (sudp1);
return;
}
/* Now we join the groups for socket1
* Group: 233.0.0.1 */
group.imr_multiaddr.s_addr = inet_addr("233.0.0.1");
group.imr_interface.s_addr = NA.IPAddr;
if (setsockopt (sudp1, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&group, sizeof(group)) < 0)
{
printf ("Error: Unable to join multicast group\n");
fdClose (sudp1);
return;
}
printf ("-----------------------------------------\n");
printf ("Socket Identifier %d has joined the following:-\n", sudp1);
printf (" - Group 233.0.0.1\n");
printf ("-----------------------------------------\n");
while (iterations < 4)
{
/* Initialize the FD Set. */
FD_ZERO(&msockets);
FD_SET(sudp1, &msockets);
/* Wait for the multicast packets to arrive. */
cnt = fdSelect( (int)sudp1, &msockets, 0, 0 , 0);
if(FD_ISSET(sudp1, &msockets))
{
cnt = (int)recv (sudp1, (void *)&buffer, sizeof(buffer), 0);
if( cnt >= 0 )
printf ("Socket Identifier %d received %d bytes of multicast data\n", sudp1, cnt);
else
printf ("Error: Unable to receive data\n");
/* Increment the iterations. */
iterations++;
}
}
/* Once the packet has been received. Leave the Multicast group! */
if (setsockopt (sudp1, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&group, sizeof(group)) < 0)
{
printf ("Error: Unable to leave multicast group\n");
fdClose (sudp1);
return;
}
/* Leave only one of the multicast groups through the proper API. */
NtIPN2Str (group.imr_multiaddr.s_addr, &buffer[0]);
printf ("Leaving group %s through IP_DROP_MEMBERSHIP\n", buffer);
/* Once we get out of the loop close socket2; this should internally leave all the groups. */
fdClose (sudp1);
printf("== End Multicast Test ==\n\n");
TaskSleep(2000);
fdCloseSession( TaskSelf() );
TaskDestroy( TaskSelf() );
}
出现的问题是,程序运行起来之后,网络可以ping通,可以收到UDP包,但是收不到来自多播组的包
不知道是什么原因?
将上面的代码添加到C6455开发板的helloworld工程中,做多播测试是可以收到多播包的。
所以代码应该没有什么问题。
我想问的是C6678的网络接收中是不是把来自多播组的包识别成非法的包了,从而收不到?
是不是和Security 模块有关?
请各位大大们帮忙看看,多谢啊!