lpcware

multicasting w/ lwip+RTOS on LPC1768

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by jnewell88 on Fri Feb 15 15:10:41 MST 2013
Hello, I'm just wondering if anyone has any luck with getting FreeRTOS + lwip to join a multicast(udp) group?
I created a thread that is able to create a socket and bind the socket, but once setsockopt is run, it exits with error -1.
Any help would be appreciated, I've been banging my head against the wall for quite some time. Code is below.

Thanks, Josh

 

static void
rtp_recv_thread(void *arg)
{        LCD_PrintString2Terminal ("Start rcv thread\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
#define STREAM_PORT             2421
#define STREAM_ADDRESS          "237.0.0.0"
  
  int sock;
  struct sockaddr_in in_addr;   /* Structure used for bind() */
  struct sockaddr_in src_addr;    /* Used to receive (addr,port) of sender */
  int src_addr_len;   /* Length of src_addr */
  struct ip_mreq mreq;
  unsigned char buffer[32];
  LWIP_UNUSED_ARG(arg);

  /* create new socket */
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
    LCD_PrintString2Terminal ("unable to create socket\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
          vTaskDelete(NULL);
    }
LCD_PrintString2Terminal ("socket created\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);

    memset(&in_addr, 0, sizeof(in_addr));   /* Zero out structure */
      /* prepare local address */
      in_addr.sin_family=AF_INET;
      in_addr.sin_addr.s_addr= htonl(INADDR_ANY);
      in_addr.sin_port=htons(STREAM_PORT);

      /* bind to local address */
      if (bind(sock, (struct sockaddr *)&in_addr, sizeof(in_addr))){
    LCD_PrintString2Terminal ("unable to bind\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
      vTaskDelete(NULL);
      }
LCD_PrintString2Terminal ("bind success\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
        // prepare multicast "ip_mreq" struct
        mreq.imr_multiaddr.s_addr = inet_addr(STREAM_ADDRESS);
        mreq.imr_interface.s_addr = htonl(INADDR_ANY);

         //join multicast group

        if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char *) &mreq, sizeof(mreq))== -1)
          { LCD_PrintString2Terminal ("cant join mcast\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
                     //EXITS HERE~
          vTaskDelete(NULL);
          }
        LCD_PrintString2Terminal ("mcast join success\n", LCD_TERMINAL_NoNL,COLOR_RED, COLOR_YELLOW);
          /* receive RTP packets */
          while(1) { //Endless loop
          }
}
}

Outcomes