title: Multicasting on Ubuntu
body: <p>My situation is as follows:
  I have a windows machine running a UDP Multicast server that is broadcasting packets. I wrote a window client that is able to capture these packets without a problem on a separate windows machine that is connected to the network. I ran into a few firewall problems on the windows machines, but worked that out.</p>
  
  <p>Now, I have an ubuntu 12.04 version of the client; however, my program isn't finding these packets. I ran through all the suggestions provided by other stack overflow posts and some google threads:</p>
  
  <ol>
  <li>when I am running my client, netstat -g shows the IP address of the multicast network</li>
  <li>I set the rp_filter to 0 using sysctl</li>
  <li>I can see the packets when using tcpdump -i wlan0 </li>
  <li>Added a route (sudo route add -net 224.0.0.0 netmask 224.0.0.0 wlan0)</li>
  </ol>
  
  <p>For step four, this is a wireless connection established over wlan0, so I add the route on wlan0. Similarly, wlan0's rp_filter=0.</p>
  
  <p>Now, for the code. From the print statements and error checking. I am seeing that I am successfully binding, joining the multicast group, creating the buffer, etc... Then it just blocks at the recvfrom() function call.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;cstring&gt;
  #include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;fstream&gt;
  
  #pragma warning( disable : 4996 )
  
  #define MAX_PACKETSIZE              100000  // max size of packet (actual packet size is dynamic)
  
  bool IPAddress_StringToAddr(char *szNameOrAddress, struct in_addr *Address);
  void Unpack(char* pData);
  
  #define MULTICAST_ADDRESS       "239.255.42.99"     // IANA, local network
  #define PORT_COMMAND            1510
  #define PORT_DATA               1511                
  #define SOCKET_ERROR -1
  #define INVALID_SOCKET -1
  
  typedef int SOCKET;
  SOCKET DataSocket;
  
  int main(int argc, char* argv[])
  {
      int retval;
      char szMyIPAddress[128] = "";
      in_addr MyAddress, MultiCastAddress;
      int optval = 0x100000;
      int optval_size = 4;
  
  
      // client address
      if(argc&gt;1)
      {
          strcpy(szMyIPAddress, argv[1]); // specified on command line
          IPAddress_StringToAddr(szMyIPAddress, &amp;MyAddress);
      }
      else
      { printf("usage: ./client [local_ip_address]\n"); return 0; }
  
      MultiCastAddress.s_addr = inet_addr(MULTICAST_ADDRESS);   
      printf("Client: %s\n", szMyIPAddress);
      printf("Multicast Group: %s\n", MULTICAST_ADDRESS);
  
      // create a "Data" socket
      printf("Create Socket.\n");
      DataSocket = socket(AF_INET, SOCK_DGRAM, 0);
  
      // allow multiple clients on same machine to use address/port
      int value = 1;
      printf("Set SO_REUSEADDR sockopt.\n");
      retval = setsockopt(DataSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&amp;value, sizeof(value));
      if (retval == SOCKET_ERROR)
      {
          close(DataSocket);
          return -1;
      }
  
      //bind
      struct sockaddr_in MySocketAddr;
      memset(&amp;MySocketAddr, 0, sizeof(MySocketAddr));
      MySocketAddr.sin_family = AF_INET;
      MySocketAddr.sin_port = htons(PORT_DATA);
      MySocketAddr.sin_addr = MyAddress;
      printf("Bind Socket.\n");
      if (bind(DataSocket, (struct sockaddr *)&amp;MySocketAddr, sizeof(struct sockaddr)) == SOCKET_ERROR)
      {
          printf("[PacketClient] bind failed.\n");
          return 0;
      }
  
      // join multicast group
      struct ip_mreq Mreq;
      Mreq.imr_multiaddr = MultiCastAddress;
      Mreq.imr_interface = MyAddress;
      printf("Join multicast group.\n");
      retval = setsockopt(DataSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&amp;Mreq, sizeof(Mreq));
      if (retval == SOCKET_ERROR)
      {
          printf("[PacketClient] join failed.\n");
          return -1;
      }
  
      // create a 1MB buffer
      printf("Create 1MB Buffer.\n");
      setsockopt(DataSocket, SOL_SOCKET, SO_RCVBUF, (char *)&amp;optval, 4);
      getsockopt(DataSocket, SOL_SOCKET, SO_RCVBUF, (char *)&amp;optval, (socklen_t*)&amp;optval_size);
      if (optval != 0x100000)
      {
          printf("[PacketClient] ReceiveBuffer size = %d\n", optval);
      } 
  
      //listening
      printf("Listening...\n");
      char  szData[20000];
      int addr_len = sizeof(struct sockaddr);
      sockaddr_in TheirAddress;
      while (1)
      {
          // Block until we receive a datagram from the network (from anyone including ourselves)
          int nDataBytesReceived = recvfrom(DataSocket, szData, sizeof(szData), 0, (sockaddr *)&amp;TheirAddress, (socklen_t*)&amp;addr_len);
  
          Unpack(szData);
      }
  
      return 0;
  }
  
  // convert ipp address string to addr
  bool IPAddress_StringToAddr(char *szNameOrAddress, struct in_addr *Address)
  {
      int retVal;
      struct sockaddr_in saGNI;
      char hostName[256];
      char servInfo[256];
      u_short port;
      port = 0;
  
      // Set up sockaddr_in structure which is passed to the getnameinfo function
      saGNI.sin_family = AF_INET;
      saGNI.sin_addr.s_addr = inet_addr(szNameOrAddress);
      saGNI.sin_port = htons(port);
  
      // getnameinfo
      if ((retVal = getnameinfo((sockaddr *)&amp;saGNI, sizeof(sockaddr), hostName, 256, servInfo, 256, NI_NUMERICSERV)) != 0)
      {
          printf("[PacketClient] GetHostByAddr failed.\n");
          return false;
      }
  
      Address-&gt;s_addr = saGNI.sin_addr.s_addr;
  
      return true;
  }
  
  void Unpack(char* pData)
  {
      printf("Begin Packet\n-------\n");
  }
  </code></pre>
  
  <p>Any suggestions are appreciated. Here is the question I used as a reference when trying to fix this problem: <a href="https://stackoverflow.com/questions/21530575/udp-socket-multicast-not-receiving-data-ubuntu">UDP socket (multicast) not receiving data (Ubuntu)</a> </p>