Java Ip Multicasting
Java Ip Multicasting
Multicast
• Unicast: point to point communication
• Broadcast: packets are sent to all
• IP supports broadcasting, but the use of broadcasts is
strictly limited
• Protocols require broadcasts only when there is no
alternative
• Routers limit broadcasts to the local network or subnet,
preventing broadcasts from reaching the Internet at large.
• Multicast: send packets to many different hosts
(a group), but not to everyone
Why Multicasting??
• Think of these
• a single email spam goes to millions of addresses
• a real-time video feed were copied to all billion+ Internet
users
• Internet crash???
8
Multicast from New York to San Francisco, Los
Angeles, and Houston
LAN2
How many times has the data
50 people crossed the internet in case of
multicasting??
Only 3 times
LAN1
20 people
LAN3
100 people
8
9
Multicast from New York to San Francisco, Los
Angeles, and Houston
How many times would the data cross the
LAN2 internet in case of point to point unicast and
50 people Broadcast??
LAN1
20 people
LAN3
100 people
9
10
Application of Multicasting
• Video conferencing: send audio-video streams to a select
group of people
• DNS routers
• News group – Usenet news
• Multiplayer games
• Distributed file systems
• Massively parallel computing
• Database replication
• Name services
• Directory services
• Router makes sure that the packets are delivered to all hosts in
the multicast group
11
12
Multicast Address
• A multicast address is the address of a group of hosts called
multicast group
• Class D Internet addresses reserved for multicast
• 224.0.0.0~ 224.0.0.255)
• reserved for routing protocols (gateway discovery)
• Multicast routers never forward datagrams with
destinations in 224.0.0.0~ 224.0.0.255
13
14
• 239.0.0.0~ 239.255.255.255
• reserved for private network (or intranet) use
14
15
Multicast Groups
• Set of Internet hosts that share a multicast address
• Data sent to the multicast address is relayed to all the members of
the group
• Group membership is open
• hosts can enter or leave the group at any time
• Groups can be permanent or transient
• Permanent groups have assigned constant address
• Most multicast groups are transient and exist only as long as they have
members
16
UDP
• Multicast data is sent via UDP
• Most multicast data is either audio or video or both (Small
data lost is fine)
• Can be three times faster than TCP
Multicast Scope
18
Datagram Format
Time-To-Live (TTL)
• Maximum number of routers that the datagram is
allowed to cross (the number of hops)
20
Use of TTL
• It guarantees that datagrams cannot travel around an internet
forever
21
22
24
Router and Routing (Cont.)
• With multicasting
• a multicast socket sends one stream of data over the
Internet to the clients’ router
• The router duplicates the stream and sends it to each of
the clients
• Without multicasting
• The server sends four separate but identical stream of
data to the router
• The router sends each of the stream to a client.
25
26
What you need to do?
1. Create a MulticastSocket
2. Have the socket join a multicast group
3. Stuff the address of the multicast group in the
DatagramPacket you want to send
4. The routers and the MulticastSocket class take care of the
rest
26
27
Multicast Sockets
• In Java, multicast data using the java.net.MulticastSocket
class
• a subclass of java.net.DatagramSocket
• Constructors
• public MulticastSocket( ) throws SocketException
• public MulticastSocket(int port) throws SocketException
• public MulticastSocket(SocketAddress bindAddress)
throws IOException
28
30
if (ni != null) {
ms.joinGroup(group, ni);
}
else
{
ms.joinGroup(group);
}
32
try {
MulticastSocket ms = new MulticastSocket(4000);
InetAddress ia = InetAddress.getByName("224.2.2.2");
ms.joinGroup(ia);
byte[] buffer = new byte[8192];
while (true) {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ms.receive(dp);
String s = new String(dp.getData());
System.out.println(s);
}
} catch (IOException ex) {
System.err.println(ex);
}
33
Sending Data to the Members of a Group
(I)
33
34
Sending Data to the Members of a Group
(II)
try {
InetAddress ia = InetAddress.getByName("experiment.mcast.net");
byte[] data = "Here's some multicast data\r\n".getBytes();
int port = 4000;
DatagramPacket dp = new DatagramPacket(data, data.length, ia,
port);
MulticastSocket ms = new MulticastSocket();
ms.send(dp);
}
catch (IOException ex) {
System.err.println(ex);
}
• default TTL : 1
34
35
Sending Data to the Members of a Group
(III)
• public void send(DatagramPacket packet, byte ttl) throws
IOException
• Sends a datagram packet to the destination, with a TTL other than the
default for the socket.
try {
InetAddress ia = InetAddress.getByName("experiment.mcast.net");
byte[] data = "Here's some multicast data\r\n".getBytes();
int port = 4000;
DatagramPacket dp = new DatagramPacket(data, data.length, ia,
port);
MulticastSocket ms = new MulticastSocket();
ms.send(dp, 64);
}
catch (IOException ex) {
System.err.println(ex);
}
35
36
try {
InetAddress ia = InetAddress.getByName("experiment.mcast.net");
byte[] data = "Here's some multicast data\r\n".getBytes();
int port = 4000;
DatagramPacket dp = new DatagramPacket(data, data.length, ia,
port);
MulticastSocket ms = new MulticastSocket();
ms.setTimeToLive(64);
ms.send(dp);
}
catch (IOException ex) {
System.err.println(ex);
}
36
37
try {
InetAddress ia = InetAddress.getByName("experiment.mcast.net");
MulticastSocket ms = new MulticastSocket();
ms.joinGroup(ia);
ms.leaveGroup(ia);
}
catch (IOException ex) {
System.err.println(ex);
}
38