Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:48:26+00:00 2026-06-06T07:48:26+00:00

I am building a overlay network system to test a protocol (Uni research). I

  • 0

I am building a overlay network system to test a protocol (Uni research). I need to join and leave multicast groups in order to receive packets from different sources.

I am unsure about the correct setup of the socket so I can close the socket and leave the multicast group and then rejoin the same multicast group later. When I try to join the same multicast group I get “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;
}

This are the functions I used for joining a group.
When leaving I simply do: (that happens inside another function)

    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

I am getting a “Address already in use” error when I try to bind the address inside the build address function.
I tried solving that by setting SO_REUSEADDR but it didn’t help, also added the DROP_MEMBERSHIP but bind still fails.

Do I need to bind in order to get it to work? What should I call or do to allow for joining and leaving of groups without this issues? I would need to do this during intervals of 30s.

Thanks a lot
M

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:48:27+00:00Added an answer on June 6, 2026 at 7:48 am

    roblem solved. Using SO_REUSEADDR when creating the groups for the first time and using DROP_MEMBERSHIP do the trick. I am not sure which one did the actual fix, as I realised I wasn’t seeing DROP_MEMBERSHIP in one case, either way I read that it is good practice to set SO_REUSEADDR always when dealing with multicast. Cheers, M

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building an inventory system. I have lots of products and each product has three
I am building a Tile Overlay server for Google maps in C#, and have
I am building a firefox extension and need to insert some elements and css
I am building an application that must add an overlay view once a scrollview
I need to include a feature on an app I'm building that allows the
I'm building a system where multiple slave processes are communicating via unix domain sockets,
I'm building a webapp that will need to provide some real-time tracking information overlayed
Building a new Mobile Web Platform for Mobile Users to purchase & download content
building a site using PHP and MySQL that needs to store a lot of
Building a Django app on a VPS. I am not very experienced with setting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.