How to UDP Broadcast with C in Linux?

Daniel Silveira picture Daniel Silveira · Dec 3, 2008 · Viewed 92.1k times · Source

How to UDP Broadcast with C in Linux?

Answer

skoylu picture skoylu · Jul 19, 2012

In many IP stack, such as Linux, this code does not work. Your socket must have broadcast permissions. Try this:

bcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
int broadcastEnable=1;
int ret=setsockopt(bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));

/* Add other code, sockaddr, sendto() etc. */