Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you should consider broadcast obsolete, specially <code>INADDR_BROADCAST</code> (255.255.255.255). Your question highlights exactly one of the reasons that broadcast is unsuitable. It should die along with IPv4 (hopefully). Note that IPv6 doesn't even have a concept of broadcast (multicast is used, instead).</p> <p><code>INADDR_BROADCAST</code> is limited to the local link. Nowadays, it's only visible use is for DHCP auto-configuration, since at such time, the client will not know yet in what network it is connected to.</p> <p>With a single <code>sendto()</code>, only a single packet is generated, and the outgoing interface is determined by the operating system's routing table (<code>ip route</code> on linux). You can't have a single <code>sendto()</code> generate more than one packet, you would have to iterate over all interfaces, and either use raw sockets or bind the socket to a device using <code>setsockopt(..., SOL_SOCKET, SO_BINDTODEVICE, "ethX")</code> to send each packet bypassing the OS routing table (this requires root privileges). Not a good solution.</p> <p>Instead, since <code>INADDR_BROADCAST</code> is not routed anyway, you can achieve almost the same thing by iterating over each interface, and sending the packet to its broadcast address. For example, assuming that your networks have 255.255.255.0 (/24) masks, the broadcast addresses are <strong>192.168.1.255</strong> and <strong>192.168.2.255</strong>. Call <code>sendto()</code> once for each of these addresses and you will have accomplished your goal.</p> <hr> <p><strong>Edit:</strong> fixed information regarding to <code>INADDR_BROADCAST</code>, and complementing the answer with information about <code>SO_BINDTODEVICE</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload