title: Multicast problem on Windows XP
body: <p>I'm testing out multicast with the two programs below. The client run well on linux and in wine on two of my machines, but it won't work properly on my windows machine (in Virtualbox).</p>
  
  <p>Strangely, if I start up vlc in windows and open the udp stream, the client program receives the packets - and when I stop vlc, the client goes silent again.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here is the the server program:</p>
  
  <pre><code>/*
   * server.c - multicast server program.
   */
  
  #include &lt;sys/types.h&gt;
  #ifdef WINDOWS
  #include &lt;winsock.h&gt;
  #include &lt;windows.h&gt;
  #else
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #endif
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  
  #define HELLO_PORT 5004
  #define HELLO_GROUP "224.0.0.1"
  
  int main(int argc, char *argv[])
  {
      struct sockaddr_in addr;
      int fd, cnt, numbytes;
      struct ip_mreq mreq;
      char message[100];
  
  #ifdef WINDOWS
       WSADATA wsaData; /* Windows socket DLL structure */
  
      if (WSAStartup(MAKEWORD(2, 0), &amp;wsaData) != 0) {
          fprintf(stderr, "WSAStartup() failed");
          return 1;
      }
  #endif
  
      /* create what looks like an ordinary UDP socket */
      if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
          fprintf(stderr, "failed to create socket.\n");
          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! */
      cnt = 0;
      while (1) {
          numbytes = sprintf(message, "%d", cnt);
          if (sendto(fd, message, numbytes, 0, (struct sockaddr*)&amp;addr,
                      sizeof(addr)) &lt; 0) {
              fprintf(stderr, "sendto failed.\n");
              return 1;
          }
  #ifdef WINDOWS
          Sleep(1000);
  #else
          sleep(1);
  #endif
          cnt++;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>and here's the client program:</p>
  
  <pre><code>/*
   * client.c -- client program for udp multicast data.
   */
  
  #include &lt;sys/types.h&gt;
  #ifdef WINDOWS
  #include &lt;winsock.h&gt;
  #else
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #endif
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  
  #define HELLO_GROUP "224.0.0.1"
  #define HELLO_PORT 5004
  #define MSGBUFSIZE 256
  
  int main(int argc, char *argv[])
  {
       struct sockaddr_in addr;
       int fd, nbytes,addrlen;
       struct ip_mreq mreq;
       char msgbuf[MSGBUFSIZE];
       u_int yes = 1;
  
  #ifdef WINDOWS
       WSADATA wsaData; /* Windows socket DLL structure */
  
      if (WSAStartup(MAKEWORD(2, 0), &amp;wsaData) != 0) {
          fprintf(stderr, "WSAStartup() failed");
          return 1;
      }
  #endif
  
      /* create what looks like an ordinary UDP socket */
      fd = socket(AF_INET, SOCK_DGRAM, 0);
      if (fd == -1) {
          fprintf(stderr, "failed to create socket.\n");
          return 1;
      }
  
  
      /* allow multiple sockets to use the same PORT number */
      if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&amp;yes, sizeof(yes)) != 0) {
          fprintf(stderr, "failed to reuse port number.\n");
          return 1;
      }
  
      /* set up destination address */
      memset(&amp;addr, 0, sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = INADDR_ANY;
      addr.sin_port = htons(HELLO_PORT);
  
      /* bind to receive address */
      if (bind(fd, (struct sockaddr*)&amp;addr, sizeof(addr)) != 0) {
          fprintf(stderr, "failed to bind socket.\n");
          return 1;
      }
  
      /* use setsockopt() to request that the kernel join a multicast group */
      mreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);
      mreq.imr_interface.s_addr = INADDR_ANY;
  
      if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&amp;mreq, sizeof(mreq)) != 0) {
          fprintf(stderr, "failed to join the multicast group.\n");
          return 1;
      }
  
      /* now just enter a read-print loop */
      while (1) {
          addrlen = sizeof(addr);
          nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0,
                  (struct sockaddr*)&amp;addr, &amp;addrlen);
  
          if (nbytes &lt; 0) {
              fprintf(stderr, "recfrom failed, %d\n", nbytes);
              return 1;
          }
  
          msgbuf[nbytes] = '\0';
          puts(msgbuf);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Oskar</p>