UDP sendto() Broadcast address not working

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

UDP sendto() Broadcast address not working

Jump to solution
5,374 Views
Tim562
Senior Contributor I

Hi All,

     I'm attempting to transmit a small (5 byte) UDP message on the broadcast I.P. address for my local network (10.0.0.255) and am not seeing any indication that the message is being sent. If I send the message to a specific address on my network (10.0.0.66) it works properly. The sendto() function is returning the proper number of bytes sent, but nobody is receiving. I have Wireshark looking for activity and I have a small utility on a PC that receives UDP messages. Both properly show received data when the UDP broadcast message is sent by another device or if my project transmits to a specific I.P. address,  neither show any activity when a broadcast message is sent by my project. I'm using RTCS 4.0.2 ported to MQX 3.8.1.1 and a Freescale MPC5125 processor. All other Ethernet communications with this project (stream sockets) works properly.

Here's a code snip.

void UDP_CallAll(void)      {      uint_32 lSockHandle, lOption;      int_32 lRetval;      sockaddr_in addr_send;      char cTxBuf[] = "Hello";

     //Create a datagram socket      lSockHandle = socket(AF_INET, SOCK_DGRAM, 0);      if(lSockHandle == RTCS_SOCKET_ERROR)           return;

     //Bind the socket      addr_send.sin_family = AF_INET;      addr_send.sin_port = 1480;      addr_send.sin_addr.s_addr = INADDR_ANY;      lRetval = bind(lSockHandle, &addr_send, sizeof(addr_send));      if(lRetval != RTCS_OK)           return;

     addr_send.sin_family = AF_INET;      addr_send.sin_port = 1480;      // addr_send.sin_addr.s_addr = 0x0A000042;  //<-- This works      addr_send.sin_addr.s_addr = 0x0A0000FF;     //<-- This doesn't (no errors indicated)

     //If RTCS_MSG_NOLOOP flag is set, the datagram is not duplicated for the local host interface      //If RTCS_MSG_NONBLOCK flag is set, calls to send() or sendto() will be non-blocking and return immediately      //----------------------------------------------------------------------------      lOption = RTCS_MSG_NOLOOP | RTCS_MSG_NONBLOCK;      lRetval = sendto(lSockHandle, cTxBuf, 5, lOption, (struct sockaddr *)&addr_send, sizeof(addr_send));      if(lRetval == RTCS_ERROR)           return;      else if(lRetval != 5)           return;

     lRetval = shutdown(lSockHandle, FLAG_ABORT_CONNECTION);

     if(lRetval != RTCS_OK)

          return;

     return;      }

Any thoughts? Is there a RTCS or MQX option that I need to configure to get this to work? Is UDP Broadcast supported? Maybe I need to Multi-cast? Thanks in advance for any tips, hints or suggestions.

Best Regards,

Tim

Labels (1)
0 Kudos
1 Solution
3,685 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Tim,

A UDP broadcast example can be found here: Re: Simple UDP communication

Hope this helps.


Have a great day,
Garabo

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
8 Replies
3,686 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Tim,

A UDP broadcast example can be found here: Re: Simple UDP communication

Hope this helps.


Have a great day,
Garabo

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
3,685 Views
Tim562
Senior Contributor I

Hi Garabo,

     Thank you for your post. I was able to get my project working with guidance from the project you posted. The only change I made was to the broadcast IP address I used. My initial code, shown above (which didn't work) used the "subnet directed" broadcast address of 10.0.0.255. I decided to do this because this IP address will usually be passed by local network routers. When I changed the broadcast address to the "limited" broadcast address of 255.255.255.255 (as shown in your example code), everything started working properly. I'm glad to have this working so I can continue development but I would like to understand why the "subnet directed" address doesn't work. I guess it's either not supported by MQX, there's a MQX/RTCS compile option I have to enable or maybe a bug? I'll look into this a little further and if I find the answer I will post it here. Thanks again for your post Garabo I appreciate it.

Best Regards,

Tim Hutchinson

0 Kudos
3,685 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Tim,

It is wired that your broadcast address isn't working. Perhaps it is not configured well in your router. For instance; for broadcasting a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is 172.16.0.0 | 0.15.255.255 = 172.31.255.255.

Have a great day,
Garabo

0 Kudos
3,685 Views
Tim562
Senior Contributor I

Hi Garabo,

     I also thought is was strange. I don't think the problem is with my network data router because subnet directed broadcast packets from one of my other projects are passed by the router which connects most of my development equipment (a small 8 port Netgear ProSafe gigabit switch). Still no resolution on my end, (255.25.255.255 works, 10.0.0.255 doesn't) but if I figure it out I will post back here. Thanks again for your help.

Best Regards,

Tim Hutchinson

0 Kudos
3,685 Views
eugeneryabtsev
Contributor III

Oh, and you have to #define RTCSCFG_IP_DISABLE_DIRECTED_BROADCAST 0 for this to work.

0 Kudos
3,685 Views
Tim562
Senior Contributor I

Hi Eugene,

     That switch RTCSCFG_IP_DISABLE_DIRECTED_BROADCAST was exactly what I was missing, it was set for 1. Would you happen to know if there is a document anywhere that defines all the switches for MQX, RTCS, etc? Thanks very much for your reply, that was great, you nailed it!

Best Regards,

Tim

0 Kudos
3,685 Views
eugeneryabtsev
Contributor III

Unfortunately, I'm not aware if this document exists.

0 Kudos
3,685 Views
eugeneryabtsev
Contributor III

What was the netmask for your 10.0.0.0 network? By default (classed network) for this network it's 255.0.0.0, so the broadcast address would be 10.255.255.255.

0 Kudos