|
我在setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)), 报错 Cannot assign requested address,有谁知道这是什么原因吗?谢谢
int send_igmp()
{
int len, status, i, leaveflag=0;
struct sockaddr_in localaddr;
struct sockaddr_in from;
struct timeval timeout;
fd_set read;
struct timeval timenew, timeold, timetemp;
long totalbytes, ratetemp, reclen;
int loop;
char destip[20];
struct ip_mreq mreq;
int s;
char buf[100];
strcpy(destip, dest);
if ((s=socket(AF_INET, SOCK_DGRAM,0))<0)
{
printf("Unable to create IGMP socket.\n");
return 1;
}
memset(&localaddr, 0, sizeof(localaddr));
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(atoi("12345"));
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
mreq.imr_interface.s_addr = inet_addr("192.168.10.10");
loop = 1;
if( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop) ) < 0 )
{
printf( "Unable to set IGMP socket option SO_REUSEADDR.\n");
return 1;
}
if ( bind( s, (struct sockaddr *)&localaddr, sizeof(localaddr) ) < 0 )
{
printf( "Unable to bind IGMP socket.\n");
return 1;
}
if ( setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq) ) < 0)
{
perror("setsockopt");
printf( "Unable to set IGMP socket option IP_ADD_MEMBERSHIP.\n");
printf( "Join multicast failed.\n" );
close(s);
return 1;
}
if(connect(s,(struct sockaddr*)&localaddr,sizeof(struct sockaddr))==-1)
{
perror("connect");
return 1;
}
close(s);
return 0;
} |
|