我正在构建一个覆盖网络系统来测试协议(统一研究)。我需要加入和离开多播组才能接收来自不同来源的数据包。

我不确定套接字的正确设置,因此可以关闭套接字并离开多播组,然后稍后再加入相同的多播组。当我尝试加入相同的多播组时,得到“ bind error: Address already in use”。

//for setting up individual groups
int setUpForGroup(struct locgro_node* node, const char* port)
{
    char mcastaddr[INET6_ADDRSTRLEN];

    struct in6_addr* full_addr_gro = &(node->group);
    if( NULL == inet_ntop(AF_INET6, full_addr_gro, mcastaddr, INET6_ADDRSTRLEN))
    {
        printf("error inet_pton, retval: \n");
        return -1;
    }

    if (buildAdd(mcastaddr, port, AF_INET6, SOCK_DGRAM, &(node->addr_st)) <0)
    {
        fprintf(stderr, "get_addr error:: could not find multicast, address=[%s] port=[%s]\n", mcastaddr, port);
        return -1;
    }

    node->sockfd = socket(AF_INET6, SOCK_DGRAM, 0);

    if (bind(node->sockfd, (struct sockaddr *)&(node->addr_st), sizeof(node->addr_st)) < 0) {
        perror("bind error:: ");
        close(node->sockfd);
        return -1;
    }

    if (joinGroup(node->sockfd, 0 , 8, &(node->addr_st)) <0) {
        close(node->sockfd);
        return -1;
    }

    return 0;
}
//internal function
int joinGroup(int sockfd, int loopBack, int mcastTTL, struct sockaddr_in6 *addr_st)
{
    int r1, r2, r3, retval;
    retval=-1;
    struct ipv6_mreq mreq6;

    memcpy(&mreq6.ipv6mr_multiaddr, &((addr_st)->sin6_addr), sizeof(struct in6_addr));

    mreq6.ipv6mr_interface= 0; // allow any interface

    //set the loopback case
    r1 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loopBack, sizeof(loopBack));
    if (r1<0) perror("joinGroup:: IPV6_MULTICAST_LOOP:: ");

/*
    setsockopt(sock_fd, IPPROTO_IPV6, SO_REUSEADDR, &mreq6, sizeof(mreq6));

    setsockopt(sock_fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &multicast_req, sizeof(multicast_req));
*/
    //set the time to live for the packets (hops)
    r2 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL));
    if (r2<0) perror("joinGroup:: IPV6_MULTICAST_HOPS::  ");

    //add this address to the group
    r3 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6));
    if (r3<0) perror("joinGroup:: IPV6_ADD_MEMBERSHIP:: ");

    if ((r1>=0) && (r2>=0) && (r3>=0)) retval=0;

    return retval;
}


//internal function
int buildAdd(const char *hostname, const char *service, int family, int socktype,  struct sockaddr_in6 *addr_st)
{
    struct addrinfo hints, *res, *ressave;
    int n, sockfd, retval;

    retval = -1;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = family;
    hints.ai_socktype = socktype;

    n = getaddrinfo(hostname, service, &hints, &res);

    if (n <0)
    {
        fprintf(stderr, "getaddrinfo error: [%s]\n", gai_strerror(n));
        return retval;
    }

    ressave = res;

    sockfd=-1;
    while(res)
    {
        sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

        if (!(sockfd < 0))
        {
            int opval = 1;
            setsockopt(sockfd, IPPROTO_IPV6, SO_REUSEADDR, &opval, res->ai_addrlen);

            if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0)
            {
            //to test that the address really is correct
                close(sockfd);
                memcpy(addr_st, res->ai_addr, sizeof(*addr_st));
                retval=0;
                break;
            }
            perror("build addr : bind error");
            close(sockfd);
            sockfd=-1;
        }
        res=res->ai_next;
    }

    freeaddrinfo(ressave); //free the struct

    return retval;
}


这是我用于加入群组的功能。
离开时,我只是做:(在另一个函数内发生)

    struct ipv6_mreq mreq6;
    memcpy(&mreq6.ipv6mr_multiaddr, &(temp_lnode->group), sizeof(struct in6_addr));
    mreq6.ipv6mr_interface= 0; // allow any interface
    setsockopt(temp_lnode->sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));

    close(temp_lnode->sockfd); //close socket


当我尝试在内部地址函数中绑定地址时,出现“地址已在使用中”错误。
我尝试通过设置SO_REUSEADDR来解决该问题,但没有帮助,还添加了DROP_MEMBERSHIP但绑定仍然失败。

我需要绑定才能使其正常工作吗?在没有这个问题的情况下,我应该打电话或做什么来允许团体的加入和离开?我需要在30秒的间隔内执行此操作。

非常感谢
中号

最佳答案

roblem解决了。首次创建组时使用SO_REUSEADDR并使用DROP_MEMBERSHIP可以解决问题。我不确定是哪一个进行了实际的修复,因为我意识到我在一种情况下没有看到DROP_MEMBERSHIP,无论哪种方式,我都读到,在处理多播时始终设置SO_REUSEADDR是一种好习惯。 M,干杯

关于c - Linux组播组-加入和离开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9966258/

10-13 09:22