How to initiate a UDP broadcast

I’m trying to figure out how I get my ME 9210 to send a UDP broadcast message out across its network.

With UDP being a connection-less protocol, currently my application waits for another host to send data to it, then uses the ‘recvfrom’ command to populate a ‘sockaddr’ struct with the sender’s address details. When responding, the ‘sendto’ command just uses the same sockaddr struct as-is.

Does the ‘sa_data’ field of the sockaddr struct just contain the sender’s IP address in the usual format (e.g. for IPv4, If so, then I guess I would send out a broadcast by just using the highest address in the subnet space, i.e. in this case. But is that right?

This is an area with which I’m not very familiar, so it would be nice to know if I’m at least on the right track before I head too far down it.


The network address in the returned structure is an unsigned long in network byte order. If you want to modify the address to send a “directed broadcast” you’ll need to grab that address, convert it to “dotted decimal format, AKA”, modify the address as needed and then reconvert back to unsigned long network byte order. Then plunk the resultant converted address into the address structure you are using for sending.

Thanks; as I don’t need to display the address at any time, converting it into a dotted decimal format seems unnecessary. Presumably I can just take the least significant byte of the address and set it to 0xFF.

Thanks for this very helpful post. Thanks.

It seems that just setting the least significant byte to 0xFF is an oversimplification which is only valid when the subnet mask is

The general case, given the host address s_addr and the subnet mask subnet_mask is apparently:

broadcast_addr = s_addr | (~subnet_mask)