Search code examples
socketsnetwork-programmingudpindyc++builder-2006

UDP Broadcasting with Indy Sockets: how to select the right Interface?


Using C++Builder2006

I'm modifying an old, small program which do these things:

  1. send a UDP broadcast message (using the 255.255.255.255 address)
  2. get the answer from all the connected devices (hardware stuffs equipped with a RabbitCore RCM3200)
  3. Optionally broadcast another UDP message containing the MAC address of one of them and the correct network configuration that that device must set for itself.

(This 'cause the device can have a wrong IP/Netmask when installed and there's a need for a method IP-independent to set it. This is a well established method from the device producer, so it cannot be changed, i'm just describing what i'm doing).

The problem i'm facing is well described here: in short, Win7 does'nt send the UDP message through all the interfaces, but only through the "preferred" one. Since the program was built using an Indy UDPClient socket (TIdUDPClient) i'd like to keep it as it is.

Questions:

  1. There's a way to "force" the UDP socket to send thru a specific Network interface? This way i could "manually" loop and send the broadcast to all the (relevant) networks
  2. If not.... what should i do to obtain the same result?

Solution

  • There's a way to "force" the UDP socket to send thru a specific Network interface?

    TIdUDPClient has a BoundIP property for that exact purpose. You will have to call TIdUDPClient.Binding.CloseSocket() each time you want to change the BoundIP, unless you bind a different TIdUDPClient object for each adapter.

    This way i could "manually" loop and send the broadcast to all the (relevant) networks

    Depending on the version of Indy you are using, you can use the TIdStack.LocalAddresses property, or the TIdStack.AddLocalAddressesToList() or TIdStack.GetLocalAddressList() method, to get a list of available local IP addresses that you can then loop through.