Linux – How to UDP Broadcast with C in Linux

linuxudp

How to UDP Broadcast with C in Linux?

Best Answer

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. */