title: Receiving Unicast and Multicast on the Same Socket in C++
body: <p>I need to receive both unicast and multicast UDP on the same socket and port at a specific IP. Based on other questions asked here, this should be possible. I can set all the options and bind successfully, but I cannot receive any traffic. It just stays blocked at <code>read()</code> call.</p>
  <p>I have tried many different options, such as <code>SO_BROADCAST</code>, <code>SO_REUSEADDR</code>, and <code>SO_REUSEPORT</code>. I have tried setting multicast group address to &quot;0.0.0.0,&quot; but that fails. I just don't know how to properly do this.</p>
  <p>Here is a stripped-down version of my current status:</p>
  <pre><code>struct sockaddr_in localSocket;
  
  int sd = socket(AF_INET, SOCK_DGRAM, 0);
  //error check here
  
  int broadcast = 1;
  //I have tried either one of these options, as well as SO_BROADCAST. 
  // Now I am trying to use both
  if ((setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&amp;broadcast, sizeof(broadcast)) &lt; 0 || 
      (setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, (char*)&amp;broadcast, sizeof(broadcast)) &lt; 0)) 
      //error handling here
  else 
  {
      memset((char*) &amp;locakSocket, 0, sizeof(localSocket));
      localSocket.sin_family = AF_INET;
      //port that I want to receive on
      localSocket.sin_port = htons(port);
      //local IP that I want to receive on. I have tried &quot;0.0.0.0&quot; here as well, 
      //but I still do not receive anything
      localSocket.sin_addr.s_addr = inet_addr(IP); 
  }
  
  if(bind(sd,(struc sockaddr*)&amp;kicakSicjetm suzeif(localSocket))&lt;0)
      //error handling
  else
  {
      //trying to join multicast here:
      struct ip_mreq group;
      //IP address that the multicast is being sent from. 
      // I have tried &quot;0.0.0.0&quot; here too, but that fails.
      group.imr_multiaddr.s_addr = inet_addr(&quot;239.255.1.1&quot;);
      //local IP that I want to receive on. 
      I have tried skipping this line, but I still do not receive any traffic
      group.imr_interface.s_addr = inet_addr(IP);
  }
  
  if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &amp;group, sizeof(group)) &lt; 0)
      //error handling
  else
  {
      while(!terminate)
      {
          //this breakpoint is hit, but I never hit any line afterwards
          numbytes = read(sd,packet,sizeof(packet));
      }
  }
  </code></pre>
  <p>Since I get to the final line, I know that none of the <code>setsocketopt()</code>, <code>bind()</code>, etc fail. I just don't ever read anything.
  Am I doing something out of order? It feels strange to <code>setsockopt</code>, <code>bind</code>, and then <code>setsockopt</code> again, but that is where my research lead me.</p>
  <p>edit: This is for a linux system.
  There were two issues:<br />
  <code>if ((setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&amp;broadcast, sizeof(broadcast)) &lt; 0 || (setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, (char*)&amp;broadcast, sizeof(broadcast)) &lt; 0)) </code><br />
  and<br />
  <code>localSocket.sin_addr.s_addr = inet_addr(IP);</code></p>
  <p>I simply commented out the pair of <code>setsockopt</code> such that I did not <code>setsockopt</code> until near the end. I was using incorrect socket options such as <code>SO_REUSEADDR</code> and <code>SO_REUSEPORT</code>. Additionally, I was binding to my local IP that I wanted to receive on instead of binding to <code>IPADDR_ANY</code>. Thanks to all for the help</p>