Java Reference
In-Depth Information
}
}
}
Example 13-2
reads the address of a multicast group, a port number, and an optional
TTL from the command line. It then stuffs the string "
Here's some multicast data
\r\n
" into the byte array
data
using the
getBytes()
method of
java.lang.String
, and
places this array in the
DatagramPacket
dp
. Next, it constructs the
MulticastSocket
ms
, which joins the group
ia
. Once it has joined the group,
ms
sends the datagram packet
dp
to the group
ia
10 times. The TTL value is set to one to make sure that this data
doesn't go beyond the local subnet. Having sent the data,
ms
leaves the group and closes
itself.
Run
MulticastSniffer
on one machine in your local subnet. Listen to the group
all-
systems.mcast.net
on port 4000, like this:
%
java MulticastSniffer all-systems.mcast.net 4000
Next, send data to that group by running
MulticastSender
on another machine in your
local subnet. You can also run it in a different window on the same machine, although
that option is not as exciting. However, you must start running the
MulticastSniff
er
before you start running the
MulticastSender
. Send to the group
all-
systems.mcast.net
on port 4000, like this:
%
java MulticastSender all-systems.mcast.net 4000
Back on the first machine, you should see this output:
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
Here's some multicast data
For this to work beyond the local subnet, the two subnets must each have multicast
routers, and the routers in between them need to have multicast enabled.