132 Chapter 4: Beyond the Basics
■
Some information is of interest to multiple recipients. In such cases, we could unicast a
copy of the data to each recipient, but this may be very inefficient. Unicasting multiple
copies over a single network connection wastes bandwidth by sending the same infor-
mation multiple times. In fact, if we want to send data at a fixed rate, the bandwidth of
our network connection defines a hard limit on the number of receivers we can support.
For example, if our video server sends 1Mbps streams and its network connection is only
3Mbps (a healthy connection rate), we can only support three simultaneous users.
Fortunately, networks provide a way to use bandwidth more efficiently. Instead of
making the sender responsible for duplicating packets, we can give this job to the network.
In our video server example, we send a single copy of the stream across the server’s con-
nection to the network, which then duplicates the data only when appropriate. With this
model of duplication, the server uses only 1Mbps across its connection to the network,
irrespective of the number of clients.
There are two types of one-to-many service: broadcast and multicast. With broadcast,
all hosts on the (local) network receive a copy of the message. With multicast, the message
is sent to a multicast address, and the network delivers it only to those hosts that have
indicated that they want to receive messages sent to that address. In general, only UDP
sockets are allowed to broadcast or multicast.
4.5.1 Broadcast
Broadcasting UDP datagrams is similar to unicasting datagrams, except that a broadcast
address is used instead of a regular (unicast) IP address. The local broadcast address
(255.255.255.255) sends the message to every host on the same broadcast network. Local
broadcast messages are never forwarded by routers. A host on an Ethernet network can
send a message to all other hosts on that same Ethernet, but the message will not be for-
warded by a router. IP also specifies directed broadcast addresses, which allow broadcasts
to all hosts on a specified network; however, since most Internet routers do not forward
directed broadcasts, we do not deal with them here.
There is no networkwide broadcast address that can be used to send a message to
all hosts. To see why, consider the impact of a broadcast to every host on the Internet.
Sending a single datagram would result in a very, very large number of packet duplica-
tions by the routers, and bandwidth would be consumed on each and every network. The
consequences of misuse (malicious or accidental) are too great, so the designers of IP left
such an Internet-wide broadcast facility out on purpose.
Even so, local broadcast can be very useful. Often, it is used in state exchange for
network games where the players are all on the same local (broadcast) network. In C#, the
code for unicasting and broadcasting is the same. To play with broadcasting applications,
simply run SendUdp.cs using a broadcast destination address.
2
Run RecvUdp.cs as you did
before (except that you can run several receivers at one time).
2
Note that some operating systems require setting SocketOptionName.Broadcast to true before
broadcasting is allowed, or an exception will be thrown. This is most likely if you are running .NET
on a UNIX-based machine using Mono.