title: How to resolve the real broadcast address for a IPv4 address with Boost.Asio
body: <p>Is there any possibility to obtain the "real local" broadcast address for a given IPv4 address with Boost.Asio?</p>
  
  <p><a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/ip__address_v4/netmask.html" rel="nofollow">ip::address_v4::netmask</a> only returns the netmask based on the IPv4 address class, so it cannot be used reliably with the overload of <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/ip__address_v4/broadcast/overload2.html" rel="nofollow">ip::address_v4::broadcast</a>.</p>
  
  <p>I need to resolve a specific broadcast address for a given (existing) host IPv4 address on the local machine, since I have trouble using an UDP endpoint with a broadcast address of 255.255.255.255.</p>
  
  <p><strong>Edit:</strong> For clarification, I do not want to resolve a hostname to an IP address or vice versa.</p>
  
  <p><strong>Edit:</strong> Here is an example:</p>
  
  <pre><code>eth0:
  
  host address: 192.168.0.1
  net mask...: 255.255.0.0 (**not** class C)
  </code></pre>
  
  <p>So I want to retrieve the broadcast address <code>192.168.255.255</code>, but the only input I want to specify is the local net address. Therefore "resolving" or "querying" is the correct term, since I do not want to calculate it.</p>
  
  <p>I have a UDPv4 server, which I bind to a specific local endpoint (by specifying the local IPv4 host address). I want to use this server in both unicast mode and broadcast mode, so I need to specify the remote endpoint as well. I am unable to do this with the "global broadcast" address returned by <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/ip__address_v4/broadcast/overload1.html" rel="nofollow">ip::address_v4::broadcast</a>, because that always returns <code>255.255.255.255</code> which leads to undesired behaviour. In addition to the host address I specify a flag to correctly set the socket option to enable broadcasting via <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/basic_datagram_socket/broadcast.html" rel="nofollow">basic_datagram_socket::broadcast</a>.</p>
  
  <p>If you have an example how-to achieve this, I would be grateful. Maybe I am thinking too complicated...</p>