title: Strange reception of multicast UDP packets (recvfrom)
body: <p><strong>EDIT: Problem explanation modified to make it clearer.</strong></p>
  
  <p>I have written 2 simple programs:</p>
  
  <ol>
  <li><p><em>Client Program</em>: which sends UDP multicast packets to a
  multicast IP address and port.</p></li>
  <li><p><em>Server Program</em>: which requests membership to the multicast IP, binds to the same port and uses 'recvfrom' to receive those packets. It also prints the content of
  those packets onto console along with source address and port.</p></li>
  </ol>
  
  <p>Now, I start the 'server' program on Machine_S and the 'client' program on Machine_C. both machines are in the same LAN.</p>
  
  <p>Client's multicast packets reach Machine_S, which I am able to see in Wireshark.
  However, 'server' program is not able to read them i.e. the call to <em>recvfrom</em> does not return.</p>
  
  <p>NOW, I start another instance of 'client' on Machine_S. As soon as this is done, the 'server' program starts receiving packets from both the clients (from both machines) and prints them out to the console!!</p>
  
  <p>At this point, even if I shut the local 'client' instance on the Machine_S, the 'server' will still continue to receive packets from the remote 'client' on Machine_C.</p>
  
  <p>So in short, the server program seems to receive packets from remote clients only when a local client is also sending packets to the same multicast address and port.</p>
  
  <p><strong>Client Code:</strong></p>
  
  <pre><code>// COMMON MACROS FOR BOTH CLIENT AND SERVER
  #define HELLO_PORT  12345
  #define HELLO_GROUP "239.0.0.50"
  
  // after initializing winsock
  
  // create UDP socket
  if ((sockid = socket (AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
  
      printf("SOCKET Error Code: %d\n", WSAGetLastError());
      WSACleanup();
      return 1;
   }
  
  
   // set up destination address
   memset(&amp;addr,0,sizeof(addr));
   addr.sin_family        =   AF_INET;
   addr.sin_addr.s_addr   =   inet_addr(HELLO_GROUP);
   addr.sin_port          =   htons(HELLO_PORT);
  
   // now just sendto() our destination!
   while (times--) {
  
       printf("Sent To : %d\n", HELLO_PORT);
  
       if (sendto (sockid, message, strlen(message)+1, 0, (struct sockaddr *)&amp;addr, sizeof(addr)) &lt; 0) {
  
           printf("SENDTO Error Code: %d\n", WSAGetLastError());
           break;
       }
  
      Sleep(500);
   }
  
   closesocket(sockid);
  
   WSACleanup();
  </code></pre>
  
  <hr>
  
  <p><strong>Server Code:</strong></p>
  
  <pre><code>// create UDP socket
  if ((sockid = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0) {
  
      printf("SOCKET Error Code: %d\n", WSAGetLastError());
      WSACleanup();
      return 1;
  }
  
  
  // set up local address
  memset(&amp;addr,0,sizeof(addr));
  addr.sin_family         =   AF_INET;
  addr.sin_addr.s_addr    =   htonl (INADDR_ANY);
  addr.sin_port           =   htons (HELLO_PORT);
  
  // bind to local address
  if (bind(sockid, (struct sockaddr *)&amp;addr, sizeof(addr)) &lt; 0) {
  
      printf("BIND Error Code: %d\n", WSAGetLastError());
      closesocket(sockid);
      WSACleanup();
      return 1;
  }
  
  // join a multicast group 
  mreq.imr_multiaddr.s_addr   =   inet_addr (HELLO_GROUP);
  mreq.imr_interface.s_addr   =   htonl (INADDR_ANY);         
  
  if (setsockopt(sockid, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&amp;mreq, sizeof(mreq)) &lt; 0) {
  
      printf("SETSOCKETOPT Error Code: %d\n", WSAGetLastError());
      closesocket(sockid);
      WSACleanup();
      return 1;
  }
  
  memset(&amp;addr,0,sizeof(addr));
  
  // now just enter a read-print loop 
  while (times--) {
  
      addrlen =   sizeof(addr);
  
      if ((nbytes = recvfrom(sockid, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&amp;addr, &amp;addrlen)) &lt; 0) {
  
          printf("RECVFROM Error Code: %d\n", WSAGetLastError());
          break;
      }
  
      inet_ntop(AF_INET, &amp;(addr.sin_addr), (PSTR)&amp;address, MSGBUFSIZE);
      srcport = ntohs(addr.sin_port);
      printf("From\t%s\t%d\t", address, srcport);
      puts(msgbuf);
  
      Sleep(1000);
  }
  
  closesocket(sockid);
  WSACleanup();
  </code></pre>
  
  <p>Few points to note:</p>
  
  <ul>
  <li>Each machine has only 1 network interface</li>
  <li>Multicast IP is 239.0.0.50 and port is 12345</li>
  </ul>
  
  <p>What could be the reason for this?</p>