Anda di halaman 1dari 19

Lecture 10: IP Multicasting 1

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???

• There is no reason to send a video feed to hosts that are not


interested in it

• Examples: need multicast


• Video conferencing: send audio-video streams to a
selected group of people
3

Sending a BBC live stream to 1000


clients
• Broadcast to the world
• Could bring the Internet to its knees
• Inefficient

• 1000 point to point unicasts


• Efficient than broadcasting
• Data is send a thousand times
• Duplicate data needlessly

Sending a BBC live stream to 1000


clients (Cont.)
• Create static connection trees
• Originating site sends data to other servers which
eventually replicate it to clients
• Each client connects to the nearest server
• Efficient than multiple unicasts
• Employed by Usenet News
• New site needs to hook into the tree manually
• Servers need to maintain many point to point
connection to their clients
5

More Efficient Way!


• Allow the routers to dynamically determine the best possible
routes

• Replicate data only when absolutely necessary

This is where multicasting comes in

Multicast: think as a group


• like a public meeting
• people can come and go
• send messages to the group and all the people in
the group will get the messages
• People not in the group will not be affected

• Half way between point to point communication


common and the broadcast
7

Multicast vs. Multiple Unicast

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??

Unicast : 170 times

Broadcast : Million times

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

Spring'15 Network Programming 10


11

How Multicasting is Done?


• Most work is done by routers and transparent to application
programmers

• An application simply sends datagram packets to a multicast IP


address

• Router makes sure that the packets are delivered to all hosts in
the multicast group

• Big problem: multicast routers are not yet ubiquitous

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

• Range 224.0.0.0 to 239.255.255.255 (256K address)


• Like any IP address, a multicast address can have a hostname
• 224.0.1.1 ntp.mcast.net (network time protocol)
13

Special Purpose Addresses


• all-systems.mcast.net (224.0.0.1)
• includes all systems that support multicasting on local subnet
• commonly used for local testing
• experiment.mcast.net (224.0.1.20)
• used for local testing

• 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

Special Purpose Addresses (Cont.)

• 239.0.0.0~ 239.255.255.255
• reserved for private network (or intranet) use

• IANA is responsible for handing out permanent multicast addresses


• About 10,000 have been assigned
• Still have 248 Million class D addresses can be used.

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

• To send data to a multicast group host puts that data in multicast


datagrams

• UDP datagrams are addressed to the multicast group


17

Multicast Scope

18

Datagram Format

TTL: Time-To-Live (1 Byte)


19

Time-To-Live (TTL)
• Maximum number of routers that the datagram is
allowed to cross (the number of hops)

• Each time a packet passes through a router, its TTL


value is decremented by at least one

• When the TTL reaches zero, the packet is discarded

• All packets would eventually be discarded

20

Use of TTL
• It guarantees that datagrams cannot travel around an internet
forever

• Source might want to intentionally limit the journey of the packet

• TTL may prevent mis-configured routers from sending packets back


and forth to each other indefinitely
Coverage of a Packet with a TTL of five

21

22

Limiting Multicast Geographically

• TTL = 0: local host


• TTL = 1: local subnet
• TTL = 16: local campus or organization
• TTL = 32: US backbone
• TTL = 48: US
• TTL = 64: North America
• TTL = 128: high bandwidth sites worldwide
• TTL = 255: All sites worldwide

[ These estimations are for datagrams originating in the


continental United States ]
23
Router and Routing
• With and without Multicasting

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

Router and Routing (Cont.)


• Real world routes can be much more complex, involving multiple
hierarchies of redundant routers

• Goal of multicast sockets


• No matter how complex the network, the same data
should never be sent more than once over any given
network
• Programmers don’t need to worry about routing issues

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

To send and receive multicast data beyond the local


subnet, you need a multicast router
• ping all-routers.mcast.net

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

Creating Multicast Sockets


• Create a Multicast Socket using any of the constructors

MulticastSocket ms = new MulticastSocket(2300);

 Open a MulticastSocket that listens on port 2300


29

Communication with a multicast group

• Four key operations


1. Join a multicast group
2. Receive data from the group
3. Send data to the members of the group
4. Leave the multicast group

30

Joining a Multicast Group (I)


public void joinGroup(InetAddress address) throws
IOException

InetAddress group =InetAddress.getByName("224.2.2.2");


ms.joinGroup(group);
31

Joining a Multicast Group (II)


public void joinGroup(SocketAddress address,
NetworkInterface interface) throws IOException

 second argument allows you to join a multicast group only on a specified


local network

SocketAddress group = new InetSocketAddress("224.2.2.2", 40);


NetworkInterface ni = NetworkInterface.getByName("eth0");

if (ni != null) {
ms.joinGroup(group, ni);
}
else
{
ms.joinGroup(group);
}

32

Receiving Data From the Group


• create a DatagramPacket with a byte array that serves as a
buffer for data
• enter a loop in which you receive the data by calling the
receive() method inherited from the DatagramSocket class

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)

• similar to sending UDP data to a unicast address


• do not need to join a multicast group
• create a new DatagramPacket
• stuff the data and the address of the multicast group into the packet
• pass it to the send()method
• Only difference is to set the TTL

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

Sending Data to the Members of a Group


(IV)
• The setTimeToLive() method sets the default TTL value
used for packets sent from the socket using the the
send(DatagramPacket dp) method

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

Leaving a Multicast Group


• void leaveGroup(InetAddress mcastaddr) throws IOException
• Leave a multicast group
• It signals the local multicast router, telling it to stop sending you
datagrams

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

Two Simple Example Program

• Multicast Sniffer Program


• Receive datagrams from a known multicast group and prints their contents
on System.out

• Multicast Sender Program


• Reads input from the command line and sends it to
a multicast group

Anda mungkin juga menyukai