Java Reference
In-Depth Information
first. If the watchdog awakens and the protocol thread has not finished, the watchdog termi-
nates the protocol thread. Unfortunately, threads killing other threads is deprecated in Java,
because the victim thread's abrupt termination might leave some objects in an inconsistent or
unrecoverable state. Since there is no other way to interrupt a blocking write() , this solution
usually will not work.
Finally, note that we could attempt to use nonblocking I/O instead of threads. Be warned,
however, that these solutions typically involve polling loops employing busy-waiting. While
adding threads does consume extra CPU and memory resources, the overhead is generally
small, especially compared to that of busy-waiting.
4.3
Multiple Recipients
So far all of our sockets have dealt with communication between exactly two entities, usually
a server and a client. Such one-to-one communication is sometimes called unicast . 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 inecient. Unicasting multiple copies over a
single network connection wastes bandwidth by sending the same information 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 eciently. 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 connection
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.3.1 Broadcast
Broadcasting UDP datagrams is similar to unicasting datagrams, except that a broadcast ad-
dress 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 mes-
sages 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 forwarded 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.
Search WWH ::




Custom Search