Java Reference
In-Depth Information
byaportnumber). Clients wanting toreceive those datagram packets create amul-
ticastsocketthatusesthatportnumber.Theyrequesttojointhegroupthrougha join
group operation thatspecifiesthespecialIPaddress.Atthispoint,theclientcanre-
ceivedatagrampacketssenttothegroup,andcanevensenddatagrampacketstooth-
ergroupmembers.Aftertheclienthasreadalldatagrampacketsthatitwantstoread,
it removes itself from the group by applying a leave group operation that specifies
the special IP address.
IPv4addresses224.0.0.1to239.255.255.255(inclusive)arereservedforuseasmul-
ticast group addresses.
Listing 9-6 presents a multicasting server.
Listing 9-6. Multicasting datagram packets
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
class MCServer
{
final static int PORT = 10000;
public static void main(String[] args)
{
try (MulticastSocket mcs = new MulticastSocket())
{
InetAddress
group
=
InetAd-
dress.getByName("231.0.0.1");
byte[] dummy = new byte[0];
DatagramPacket dgp = new DatagramPacket(dummy, 0,
group, PORT);
int i = 0;
while (true)
{
byte[] buffer = ("line "+i).getBytes();
dgp.setData(buffer);
Search WWH ::




Custom Search