Unix Network Programming Manual
Broadcasting Messages
An interesting problem that occasionally arises in network programming is
trying to send a message to a group of hosts where you may or may not
know there addresses. If you know the addresses, you can simply send a
message to each one indivdually, although that could be an arduous task if
there are many of them. On the other hand, if you don't know the host
addresses that is impossible. For example, if you know that there are one
or more hosts on your network that run a database server and you would like to
send a message to those servers.
If you don't know the host address(es) or if you just want to send a message
to several hosts at one time, you want to send a broadcast message.
Such a message is sent with an address that all the hosts will accept and
process as though it were their own. If the message has the port number
that a process has bound an address to, it will be delivered to that process.
If there is no such process, the message will be discarded. If
the wrong program has bound the port, it will get the message. That's why
it is important to make your network programs bullet-proof.
In order to send broadcast messages, you need to tell the operating system
that this is what you want to do. This is done by setting the socket
options:
/*
* Set the socket to handle broadcast messages.
*/
int sockopt;
sockopt = 1;
if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST,
(char *) &sockopt, sizeof(int)) < 0)
{
perror ("on set socket options");
exit(-1);
}
and then use a broadcast address which is interpreted as being all hosts on
a particular network. If a network has a class B address, such as 155.55,
then 155.55.0.0 would be all hosts on that network. If the network is
subnetted, such as a portion of the network that has addresses
155.55.192, then the broadcast address would be 155.55.192.0.
All ones in the host portion means that all hosts that match the other part
are to receive and process. For example,
addr.sin_port = htons (PORT);
addr.sin_addr.s_addr = inet_addr ("155.55.192.0");
sendto (mysock, buf, strlen (buf) + 1, 0, &addr, sizeof (addr));
will send buf to the server at on every host on the local
network. Broadcasts are always datagrams, because you need
a specific IP address to make the connection for a stream.
An interesting source of confusion is that while all ones indicates a
broadcast address to IP, the software for sockets was originally written
to use zeros to indicate broadcast. That situation hasn't changed.
Broadcasts can be a problem if used indiscriminately. For example, broadcasting
to an entire part of the network when it isn't necessary, or broadcasting
on a frequent basis. You should use broadcasts when necessary, and to the
minimum set of hosts that must be included.
One problem that must be addresses is the return messages from a broadcast.
You have no control over who will respond, the order in which they will
respond, or the time period. If you use broadcasting, be prepared for
the worst possible situations.