[英]Sendto returns -1 and errno 22 (Invalid argument), when set the multicast outgoing interface by ip_mreqn
我遇到一个问题,当我尝试通过下面的代码设置所需的传出接口来发送到多播组时,实际上当条件为TRUE (if(config.enable_if == 1))
,sendto系统调用返回错误Invalid Argument
,但如果条件为False,则sendto发送数据并且不会产生任何错误。
请任何人有一个主意,还是应该修改我的代码中的任何内容?
/* 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);
sendto
失败的原因之一是因为您向它传递了它不期望的数据指针。 如果您有char* databuf
然后执行&databuf
则会得到char**
类型的指针的地址,即指向指针的指针。 如果删除强制类型转换(不需要),则在编译时至少会收到警告甚至错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.