AnsweredAssumed Answered

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

Question asked by yongchao yue on Oct 7, 2014
Latest reply on Dec 19, 2014 by Martin Latal

in linux ,I saw  this"

  1. nb=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char*)&opt,sizeof(opt));

"

but I can't find SO_BROADCAST has been defined in mqx,

how to config to make udp to receive a broadcast datagram.

my code : 

int connect_UDP()

{

 

  uint_32 cmd_reply_len;

 

    uint_32         sock,sendsock;

  uint_16 rlen;

   

    IPCFG_IP_ADDRESS_DATA    ip_data;

    _enet_address            enet_addr = ENET_MAC;

   

    sockaddr_in     addr;

    sockaddr_in dest_addr;

    uint_32         error, option;

    MQX_FILE_PTR   sockfd, telnetfd;

    _mqx_uint       echoflag = IO_SERIAL_ECHO;

 

 

    char senddata[128] = "mesh gate connected!\r\n";

    char        recvdata[512];

    int count;

    int             num,pRcv;

   

   

   

//    _RTCSPCB_init          = 3;

//    _RTCS_msgpool_init     = 3;

//    _RTCS_socket_part_init = 3;

 

 

 

    memset(recvdata,0,sizeof(recvdata));

   

    error = RTCS_create();

    if (error != RTCS_OK)

    {

       fputs("Fatal Error: RTCS initialization failed.", stderr);

       _task_block();

    }

    ip_data.ip = ENET_IPADDR;

    ip_data.mask = ENET_IPMASK;

    ip_data.gateway = ENET_IPGATEWAY;

   

   

   

    ENET_get_mac_address (BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_addr);

 

 

   

    error = ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_addr);

    if (error != IPCFG_OK)

    {

        fprintf(stderr, "Fatal Error 0x%X: Network device initialization failed.\n", error);

        _task_block();

    }

   

 

 

    error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data);

    if (error != IPCFG_OK)

    {

        fprintf(stderr, "Fatal Error 0x%X: IP address binding failed.", error);

        _task_block();

    }

   

   

    /* Install device drivers for socket and telnet I/O */

    //_io_socket_install("socket:");

 

 

  sendsock = socket(PF_INET, SOCK_DGRAM, 0);

   

    /* Bind the socket to the Telnet port */

    addr.sin_family      = AF_INET;

    addr.sin_port        = 5055;

    //addr.sin_addr.s_addr = ENET_IPADDR;   //INADDR_BROADCAST

  addr.sin_addr.s_addr = INADDR_ANY;

 

 

    dest_addr.sin_family = AF_INET;

    dest_addr.sin_port        = 5055; 

    dest_addr.sin_addr.s_addr = IPADDR(192,168,1,112);

 

 

  option = 1;

  setsockopt(sendsock,SOL_SOCKET,OPT_RECEIVE_PUSH,&option,sizeof(option));

 

    error = bind(sendsock, &addr, sizeof(addr)); //return_error_if(error != RTCS_OK)

 

  _time_delay(100);

  rlen = sizeof(dest_addr);

  //sendto(sendsock, senddata, strlen(senddata), 0,&dest_addr, rlen);

  while(1)

  {

  count = recvfrom(sendsock, recvdata, 512, 0, (struct sockaddr *) &dest_addr,&rlen);

  if(count == RTCS_ERROR)

  {

  ;

  }

  else

  {

  if(count>0)

  {

  //_io_write(serial4_fd, recvdata, count);

  //_time_delay(1000);

  //memset(recvdata,0,sizeof(recvdata));

  //count = 0;

 

  //cmd_reply_len = phone_cmd_analsys((uint_8*)recvdata,count);

  //sendto(sendsock, phone_reply, cmd_reply_len, 0,&dest_addr, rlen);

  sendto(sendsock, "123", 3, 0,&dest_addr, rlen);

  memset(phone_reply,0,PHONE_REPLY_LEN);

  count = 0;

  }

  }

  }

}

Outcomes