Setting the SO_BROADCAST Option
The topic of broadcasting with UDP has not been covered yet. However, it should be easily appreciated that the use of a broadcasting capability could be misused and cause grief on the affected networks. To avoid broadcasting when broadcasting wasn't intended, the socket is creating with the broadcasting feature disabled. If broadcasting is truly intended, then the C programmer is expected to take the trouble to enable this feature for the socket first.
The topic of broadcasting will be covered in Chapter 13, "Broadcasting with UDP."
Consequently, only the option itself will be described here. The SO_BROADCAST option is a Boolean flag option, which is defined, fetched, and set with the int data type. The following example shows how to enable the SO_BROADCAST option:
#define TRUE 1
#define FALSE 0
int z; /* Status code */
int s; /* Socket s */
so_broadcast = TRUE;
z = setsockopt(s,
if ( z )
If the setsockopt(2) function returns zero, the socket s has been enabled to perform broadcasting. Note, however, that the socket type chosen must be one that is capable of broadcasting, such as a UDP socket.