I faced an issue, when I try to send to multicast group by setting the intended outgoing interface by the code bellow, Actually when the condition is TRUE (if(config.enable_if == 1))
the sendto system call returns error Invalid Argument
, but if the condition was False sendto send data and doesn't generate any error.
我遇到了一个问题,当我尝试通过以下代码设置预期的传出接口来发送到多播组时,实际上当条件为TRUE时(if(config.enable_if == 1)),sendto系统调用返回错误无效参数,但如果条件为False send则发送数据并且不会产生任何错误。
Please Anyone has an idea, or should I modify anything in my code?
请问任何人有想法,还是应该修改我的代码中的任何内容?
/* Create a datagram socket on which to send. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local */
/* multicast capable interface. */
if(config.enable_if == 1){
mreqn.imr_ifindex = if_nametoindex("eth3");
rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn));
}
/* Initialize the group sockaddr structure with a */
/* group address of dynamic address and port dynamic port. */
memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr(config.mip);
groupSock.sin_port = htons(config.port);
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
rc = sendto(sd, (const char *)& databuf, datalen, 0, (const struct sockaddr*)&groupSock, sizeof (struct sockaddr));
printf("errno %d\n",errno);
1 个解决方案
#1
1
One reason sendto
fails is because you pass it a data pointer it does not expect. If you have char* databuf
and you then do &databuf
you get the address of the pointer, i.e. a pointer to a pointer, of type char**
. If you remove the cast (which is not needed) then you will get at least a warning or maybe even an error when compiling.
sendto失败的一个原因是因为你传递了一个它不期望的数据指针。如果你有char * databuf然后你做&databuf你获得指针的地址,即指向char **的指针的指针。如果您删除了演员表(不需要),那么编译时至少会出现警告或甚至是错误。
#1
1
One reason sendto
fails is because you pass it a data pointer it does not expect. If you have char* databuf
and you then do &databuf
you get the address of the pointer, i.e. a pointer to a pointer, of type char**
. If you remove the cast (which is not needed) then you will get at least a warning or maybe even an error when compiling.
sendto失败的一个原因是因为你传递了一个它不期望的数据指针。如果你有char * databuf然后你做&databuf你获得指针的地址,即指向char **的指针的指针。如果您删除了演员表(不需要),那么编译时至少会出现警告或甚至是错误。