title: Subscribing to multiple multicast groups on one socket (Linux, C)
body: <p>Is it possible to receive data from more than one multicast group on a single socket?</p>
  
  <p>For example:</p>
  
  <pre><code>void AddGroup(int sock,
                const char* mc_addr_str,
                int mc_port,
                const char* interface) {
    struct sockaddr_in mc_addr;
    memset(&amp;mc_addr, 0, sizeof(mc_addr));
    mc_addr.sin_family      = AF_INET;
    mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
    mc_addr.sin_port        = htons(mc_port);
  
    if ((bind(sock, (struct sockaddr *) &amp;mc_addr,
              sizeof(mc_addr))) &lt; 0) {
      perror("bind() failed");
      exit(1);
    }
  
    // construct an IGMP join request structure
    struct ip_mreq mc_req;
    mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
    mc_req.imr_interface.s_addr = inet_addr(interface);
  
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                    (void*) &amp;mc_req, sizeof(mc_req))) &lt; 0) {
      perror("setsockopt() failed");
      exit(1);
    }
  }
  </code></pre>
  
  <p>This code works when I add one multicast group. But when I try to add another, the "bind" fails. I don't quite understand why the bind needs to be there in the first place? (but the code doesn't work without it).</p>
  
  <p>Ideally I would like to call AddGroup multiple times on the same socket. Is this possible? Or do I need one socket per group and then just use polling?</p>