
m 5.6 Multiple Recipients
77
9 Check the socket descriptors: lines 72-77
Test each descriptor, accepting and handling the valid connections.
5. Wrap up: lines 82-86
Close all ports and free memory.
select
() is a powerful function. It can also be used to implement a timeout version of any of
the blocking I/O functions (e.g., recv(), accept ()) without using alarms.
5.6 Multiple Recipients
So far, all of our sockets have dealt with communication between two entities, usually a server
and a client. Such one-to-one communication is called
unicast
because only one Cuni") copy
of the data is sent ("cast"). In some cases, information is of interest to multiple recipients. We
could simply unicast a copy of the data to each recipient; however, this may be very inefficient.
Consider the case where the sender connects to the Internet over a single path. Unicasting
multiple copies over that single connection creates duplication, wasting bandwidth. In fact, if
each unicast connection across this shared path requires a fixed amount of bandwidth, there
is a hard limit to the number of receivers we can support. For example, if a video server sends
1-Mbps streams and the server's network connection is only 3 Mbps (a healthy connection
rate), it can only support three simultaneous users.
Fortunately, the network provides a way to more efficiently use bandwidth. Instead of
making the sender responsible for duplicating packets, we can give this job to the network.
In our video server example, we send only a single copy of the stream across the server's
connection to the network, which duplicates the data only when appropriate. With this model
of duplication, the server uses only 1 Mbps across its connection to the network, irrespective
of the number of clients.
There are two types of network duplication:
broadcast
and
multicast.
With broadcast, all
hosts on the network receive a copy of the message. A broadcast message is indiscriminately
sent to everyone on the network. A multicast message is sent to some (potentially empty)
subset of all the hosts on the network. Obviously, broadcast is just a special case of multicast,
where the subset of receivers contains all of the hosts on the network. For IP,
only UDP sockets
are allowed to broadcast and multicast.
5.6.1 Broadcast
Broadcasting UDP datagrams is similar to sending unicast datagrams. The main distinction
between the use of broadcast and unicast is the form of the address. In practice, there are
two types of broadcast address:
local broadcast
and
directed broadcast.
A 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 LAN can
send a message to all other hosts on that same LAN, but the message will not be forwarded
by a router so no other hosts may receive it. Directed broadcast allows broadcast to all hosts