Broadcasting UDP packet to 255.255.255.255 Broadcasting UDP packet to 255.255.255.255 linux linux

Broadcasting UDP packet to 255.255.255.255


I just ran into this issue. Everything worked fine until I removed the gateway and then I get a network unreachable error when trying to send from the socket.

Since I am using the broadcast messages to configure the network settings of my box it is not an alternative for me to configure the gateway address to get it working. However since my box only has a single network interface I found the following solution:

// Bind to the proper interface to be able to send without default gatewaychar netif[] = "eth0";setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, netif, sizeof(netif));

With this I am able to send to the broadcast 255.255.255.255 address on the socket regardless of any default gateway. This solution should work with multiple network interfaces as long as you know what interface you want to use.


Broadcast by definition is limited to one subnet. It would be horrifying to see broadcast escape one's subnet and overflow the internet. If you are looking for a way to cross subnets, you should take a look at Multicast. Also check this. Good luck!


Please check your default gateway. If gateway's IP is in another subnet you would get error "Network is unreachable." after some time of broadcasting to 255.255.255.255 address, by the way sending directly to subnet broadcast (192.168.1.255 if your ip is 192.168.1.1 for example) will be work correctly even if gateway IP is in another subnet.