IPv6套接字C - 它不会加入多播组

时间:2017-05-16 18:04:52

标签: c sockets ipv6 multicast

我正在尝试加入多播组但它根本不起作用,我实际上看不出代码有什么问题。

这是代码,这就是我执行程序的方式

./ suscriptor f15 :: 33 eth0 4343

int main(int argc, char **argv)
{
    int                    listenfd, connfd, n;
    socklen_t              len;
    char                   buff[MAXLINE];
    time_t                 ticks;
    struct sockaddr_in6    servaddr, cliaddr;
    extern const struct in6_addr    in6addr_any;
    char equipo [INET6_ADDRSTRLEN];

    struct ipv6_mreq ipv6mreq;

    int uno = 1;

    if ((listenfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
        perror ("Error en socket\n");
        exit (-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin6_family = AF_INET6;
    servaddr.sin6_addr = in6addr_any;

    if (argc != 4) {
        servaddr.sin6_port = htons(4343);   //default port
    }
    else {
        servaddr.sin6_port = htons(atoi(argv[3]));    /* daytime server */
        printf("puerto:  %d\n",atoi(argv[3]));

    }    

    if (argc != 4) {
        ipv6mreq.ipv6mr_interface = if_nametoindex("eth0");
    }
    else {
        ipv6mreq.ipv6mr_interface = if_nametoindex(argv[2]);
        printf("Interfaz %d \n",ipv6mreq.ipv6mr_interface);
    }

    if (argc != 4) {
        if(inet_pton(AF_INET6,"ff15::33",&ipv6mreq.ipv6mr_multiaddr)==-1){
            perror("Llamada inet_pton\n");
            exit(1);
        }
    }
    else {
        if(inet_pton(AF_INET6,argv[1],&ipv6mreq.ipv6mr_multiaddr)==-1){
            perror("Llamada inet_pton\n");
            exit(1);
        }
        printf("direccion: %s\n", argv[1]);
    }

    if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &uno, sizeof(uno)) < 0 ){
         ("error");
    }

    if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) < 0) {
        perror ("Error en bind\n");
        exit (-1);
    }

    /* joining the multicast group */
    if(setsockopt(listenfd,IPPROTO_IPV6,IPV6_ADD_MEMBERSHIP,&ipv6mreq,sizeof(ipv6mreq))==-1){
        perror("Llamada setsockopt para multicast\n");
        exit(1);
    }
}

一些帮助将不胜感激。

感谢。

0 个答案:

没有答案