UDP Broadcast not working when no Default Gateway configured on Linux

udp_broadcastThere are a few pitfalls for the burdened Software Engineer getting going with UDP datagram broadcast. One that I hit this week involved clients not receiving broadcast messages unless a default gateway was configured on the broadcasting host.

Very strange, why should broadcast need a gateway? Well it turns out that it doesn’t, it really just needed to know which network adapter to use when sending (and failing that it uses the gateway).

So to fix this we have a few options, including:

1.) Add a broadcast route to the network device in question (e.g. in /etc/network/interfaces), or

2.) Update the UDP code so that the outgoing network device is specified – which can be done like this (to target eth0):

  char buffer[]="eth0";
  setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, buffer, 5);

PS, I think this may be non-portable an may only work on Linux. Some more info here.

Broadcasting from a muddy field!

Ever wondered where the term ‘broadcast’ comes from? Well according to a programme on the Beeb called ‘Victorian Farm‘ (which i’ve been watching!) it has slightly startling rural roots – it seems that it originally referred to a method of sowing seed by hand where the sower casts handfuls in a broad arc across his body as he walks – hence he’s broadcasting! Apparently it was _the_ method sowing seed before the first mechanical seed drills became available.


PS. The Victorian Farm is an interesting and mildly amusing programme in which a group of experimental arciologists dressed up, lived and worked on a ‘Victorian Farm’ for a year – well worth a look if it’s repeated which I’m sure it will be just like all other television these days!