Java Reference
In-Depth Information
How It Works
Multicasting is the ability to broadcast a message to a group of listeners in a single
transmission. A good analogy of multicasting is radio. Thousands of people can tune
into a single broadcast event and listen to the same message. Computers can do similar
things when sending messages to listeners. A group of client machines can tune into
the same address and port number to receive a message that a server broadcasts to that
address and port. The Java language provides multicasting functionality via datagram
messaging. Datagrams are independent, nonguaranteed messages that can be delivered
over the network to clients. (Being nonguaranteed means that the arrival, arrival time,
and content are not predictable.) Unlike messages sent over TCP, sending a datagram is
a nonblocking event, and the sender is not notified of the receipt of the message. Data-
grams are sent using the User Datagram Protocol (UDP) rather than TCP. The ability to
send multicast messages via UDP is one benefit over TCP, as long as the ordering, reli-
ability, and data integrity of the message are not mission-critical.
Java facilitates multicast messaging via the MulticastChannel interface.
Classes that implement the MulticastChannel interface have multicasting enabled
and can therefore broadcast to groups and receive group broadcasts. One such class is
the DatagramChannel , which is a selectable channel for datagram-oriented sockets.
In the solution to this recipe, both a client and a server program are used to communic-
ate via multicast messaging, and the DatagramChannel class is used on both sides
of the communication. A DatagramChannel must be configured in a specific way if
it is to be used for accepting multicast messages. Specifically, there are options that
need to be set on the DatagramChannel client that is opened. We will discuss those
options shortly. The following steps are required for creating a client that receives mul-
ticast messages.
1.
Open a DatagramChannel .
2.
Set the DatagramChannel options that are required to multicast.
3.
Join the client to a multicast group and return a MembershipKey
object.
4.
Open the client.
In the solution to this recipe, the client application begins by obtaining a reference
to the network interface that will be used for receiving the broadcast messages. Setting
up a NetworkInterface is required for multicasting. Next, a port number is
Search WWH ::




Custom Search