Generally Multicast Sockets should be bound to the wildcard address [INADDR_ANY], before joining the group. Once the binding is done, you can join to the desired group

by calling SetSockOpt API with IP_ADD_MEMBERSHIP providing the IP address of the desired interface and the ip address of the multicast group to join. Below code shows

you how to do that:

   1: SOCKET s;
   2:  
   3: SOCKADDR_IN localif;
   4:  
   5: struct ip_mreq mreq;
   6:  
   7: s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   8:  
   9: localif.sin_family = AF_INET;
  10:  
  11: localif.sin_port = htons(29309);
  12:  
  13: localif.sin_addr.s_addr = htonl(INADDR_ANY);
  14:  
  15: //First Bind to WildCard Address.
  16:  
  17: int ret = bind(s, (SOCKADDR *)&localif, sizeof(localif));
  18:  
  19: if(ret!=0)
  20:  
  21: {
  22:  
  23: printf("Bind %d\n",WSAGetLastError());
  24:  
  25: return 0;
  26:  
  27: }
  28:  
  29: //Add to the desired multicast group on the desired interface by calling SetSockOpt.
  30:  
  31: mreq.imr_interface.s_addr = inet_addr("172.17.2.153");
  32:  
  33: mreq.imr_multiaddr.s_addr = inet_addr("224.1.99.99");
  34:  
  35: setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));

-Balajee P

Windows SDK