Presentation is loading. Please wait.

Presentation is loading. Please wait.

Block 14 Group Communication (Multicast)

Similar presentations


Presentation on theme: "Block 14 Group Communication (Multicast)"— Presentation transcript:

1 Block 14 Group Communication (Multicast)
Jin Sa 2018/7/5 Client-server programming

2 Client-server programming
Outline Concepts Java basic multicast implementation Program examples 2018/7/5 Client-server programming

3 Client-server programming
Unicast vs. Multicast 2018/7/5 Client-server programming

4 Multicast applications
Multicast allows the efficient distribution of information between a single multicast source and multiple receivers. Examples of multicast applications are audio/video services such as IPTV and conferencing services such as NetMeeting 2018/7/5 Client-server programming

5 Client-server programming
Multicast group A set of processes form a group, called a multicast group. The processes can be on different hosts on different networks. Each process in a group can send and receive message. A message sent to the group can be received by each participating process in the group. 2018/7/5 Client-server programming

6 Client-server programming
Multicast operations Primitive operations: join, leave, send and receive Note that joining a group means that the process is entitled to receive a message sent to the group. It still needs to issue a receive operation in order to receive messages. 2018/7/5 Client-server programming

7 IP Multicast addresses - 1
Instead of a single process, a multicast datagram is meant to be received by all the processes that are currently members of a specific multicast group. Hence each multicast datagram needs to be addressed to a multicast group instead of an individual process. A multicast address is the shared address of a group. A multicast group is specified by a multicast IP address, and a port number. 2018/7/5 Client-server programming

8 IP Multicast addresses - 2
The multicast addresses are in the range of to (The address is reserved and should not be used by any application. ) 2018/7/5 Client-server programming

9 The Java Basic Multicast API -1
At the transport layer, the basic multicast supported by Java is an extension of UDP (the User Datagram Protocol) Datagram - recap 2018/7/5 Client-server programming

10 The Java Basic Multicast API - 2
In addition to the classes provided for datagram, Java provides the MulticastSocket class A MulticastSocket is a DatagramSocket, with additional capabilities for joining and leaving a multicast group. 2018/7/5 Client-server programming

11 Implementing multicast operations in Java
2018/7/5 Client-server programming

12 Joining a multicast group
// join a Multicast group // at port 3456 and // IP address InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); s.joinGroup(group);   2018/7/5 Client-server programming

13 Sending message to a multicast group
InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); String msg = "a multicast message."; DatagramPacket hi = new DatagramPacket(msg.getBytes( ), msg.length( ),group, 3456); s.send(hi); 2018/7/5 Client-server programming

14 Receiving messages sent to a multicast group
InetAddress group= InetAddress.getByName(" "); MulticastSocket s=new MulticastSocket(3456); s.joinGroup(group); byte[] buf = new byte[1000]; DatagramPacket recv = new DatagramPacket(buf,buf.length); s.receive(recv); 2018/7/5 Client-server programming

15 Leaving a multicast group
MulticastSocket s = new MulticastSocket(3456); … … s.leaveGroup(group); 2018/7/5 Client-server programming

16 Time-To-Live (TTL) for Multicast Packets -1
Need to propagate a multicast message from a host to a neighboring host delivering the message to all the participants. Uses the Time To Live (ttl) parameter to decide how "far" from a sending host a given multicast packet should be forwarded. 2018/7/5 Client-server programming

17 Time-To-Live (TTL) for Multicast Packets - 2
The time-to-live (ttl) parameter limits the count of network links or hops that the packet will be forwarded on the network. The value for ttl can range from 0 to 255. The larger the ttl, the greater the distance a data packet will travel. 2018/7/5 Client-server programming

18 Time-To-Live (TTL) for Multicast Packets - 3
  MulticastSocket s = new MulticastSocket(3456); s.setTimeToLive(1); 2018/7/5 Client-server programming

19 Time-To-Live (TTL) for Multicast Packets - 4
The recommended ttl settings are: 0 localhost 1 local network 32 same site 64 same region 128 same continent 255 unrestricted, i.e. the world 2018/7/5 Client-server programming

20 Multicast program examples
Example1Sender Example1Receiver Example 2 Student activity Example3 OneLinerChatroom 2018/7/5 Client-server programming

21 Client-server programming
Example1 Example1Sender Set a multicast group address Create a multicast socket on a port Create a datagram packet to be sent to the group Send the datagram packet using the multicast socket Example1Receiver Set the same multicast group address Create a multicast socket with the same port number Join the group Create a datagram packet as a placeholder Issue a receive method to receive a data packet. To run: (needs internet) run Example1Receiver several times to have a few participants in the group (internet on!!!) Run Example1Sender Source code on E:clientserverprogramming0708/multicast/MultiExample1sender and MultiExample1receiver 2018/7/5 Client-server programming

22 Fragment of code for Example1Sender (hand out)
InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); String msg="Hi everyone!"; DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), group,3456); s.send(packet); 2018/7/5 Client-server programming

23 Fragment of code for Example1Receive (hand out)
InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); s.joinGroup(group); byte[] buf = new byte[100]; DatagramPacket recv = new DatagramPacket(buf, buf.length); s.receive(recv); System.out.println(new String(recv.getData())); 2018/7/5 Client-server programming

24 Client-server programming
Student activity Example1Sender_loop Set a multicast group address Create a multicast socket on a port Loop Get message from scanner Create a datagram packet containing the message to send to group Send the datagram packet using the multicast socket Example1Receiver_loop Set the same multicast group address Create a multicast socket with the same port number Join the group Create a datagram packet as a placeholder Issue a receive method to receive a data packet. 2018/7/5 Client-server programming

25 Student activity: Example1Receiver_loop_1
public class Example1Receiver_loop_1 { public static void main(String[] args) { try { System.out.println("Enter the process name : "); Scanner in = new Scanner(System.in); String processName = in.nextLine(); InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); s.joinGroup(group); byte[] buf = new byte[100]; DatagramPacket recv = new DatagramPacket(buf, buf.length); // see the next slide // Loop // * Issue a receive method to receive a data packet // * display message. } }// end main }// end class Set up and join a Multicast group Set up DatagramPacket for receiving message 2018/7/5 Client-server programming

26 Student activity: Example1Receiver_loop_1
public class Example1Receiver_loop_1 { public static void main(String[] args) { // see the previous slide // * Set a multicast group address // * Create a multicast socket on a port int count = 0; while (count < 10) { s.receive(recv); System.out.println(new String(recv.getData())); System.out.println("received from:" + recv.getAddress() + " and port no: " + recv.getPort()); } s.close(); } catch (Exception ex) { ex.printStackTrace(); }// end main }// end class Loop Issue a receive method to receive a data packet. Display message received 2018/7/5 Client-server programming

27 Student activity: Example1Sender_loop_1
public class Example1Sender_loop_1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); try { InetAddress group = InetAddress.getByName(" "); MulticastSocket s = new MulticastSocket(3456); // See the next slide // Loop // * Get message from scanner // * Create a datagram packet containing the message to send to group // * Send the datagram packet using the multicast socket } s.close(); } catch (Exception ex) { ex.printStackTrace(); } }// end main }// end class Create a Multicast group 2018/7/5 Client-server programming

28 Student activity: Example1Sender_loop_1
public class Example1Sender_loop_1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); try { // See the previous slide // * Set a multicast group address // * Create a multicast socket on a port int count=1; while (count<10) { System.out.println("Enter the text for sendingto the group: "); String msg = scan.nextLine(); DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), group, 3456); s.send(packet); System.out.println(msg +" has been sent."); count++; } s.close(); } catch (Exception ex) { ex.printStackTrace(); } }// end main }// end class Loop Get message via keyboard. Set up Datagram Packet containing the message Send datagramPacket 2018/7/5 Client-server programming

29 Student activity: Example1Receiver_loop_1
public class Example1Receiver_loop_1 { public static void main(String[] args) { try { System.out.println("Enter the process name : "); Scanner in = new Scanner(System.in); String processName = in.nextLine(); // see the next slide // Loop // * Issue a receive method to receive a data packet // * display message. } }// end main }// end class Set up and join a Multicast group Set up DatagramPacket for receiving message 2018/7/5 Client-server programming

30 Student activity: Example1Receiver_loop_1
public class Example1Receiver_loop_1 { public static void main(String[] args) { // see the previous slide // * Set a multicast group address // * Create a multicast socket on a port int count = 0; while (count < 10) { } s.close(); } catch (Exception ex) { ex.printStackTrace(); }// end main }// end class Loop Issue a receive method to receive a data packet. Display message received 2018/7/5 Client-server programming

31 Student activity: Example1Sender_loop_1
public class Example1Sender_loop_1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); try { // See the next slide // Loop // * Get message from scanner // * Create a datagram packet containing the message to send to group // * Send the datagram packet using the multicast socket } s.close(); } catch (Exception ex) { ex.printStackTrace(); } }// end main }// end class Create a Multicast group 2018/7/5 Client-server programming

32 Student activity: Example1Sender_loop_1
public class Example1Sender_loop_1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); try { // See the previous slide // * Set a multicast group address // * Create a multicast socket on a port int count=1; while (count<10) { System.out.println(msg +" has been sent."); count++; } s.close(); } catch (Exception ex) { ex.printStackTrace(); } }// end main }// end class Loop Get message via keyboard. Set up Datagram Packet containing the message Send datagramPacket 2018/7/5 Client-server programming

33 Exercises for the practicals
Implement example 1 on slide 21 Implement student activity on slide 24 (Option) Implement the OneLinerChatroom example on slide 34. 2018/7/5 Client-server programming

34 Example2– a very simple oneliner chatroom (Option)
Everyone in the group does the same thing: Each person joins a group After joining, each person can send one message to the group After joining, each person can receive all the messages sent to the group. 2018/7/5 Client-server programming

35 Oneliner chatroom:Two class
MultiExample2SenderReceiver Get the participant’s name Set a multicast address and port number for the group Create a separate thread (MultiReadThread) to receive multicast messages repeatedly from other members Send the message “hello” once to the group by creating a datagram packet creating a multicast socket and issue send MultiReadThread Set the same multicast group address as everyone else Create a multicast socket with the same port number Joint the group Repeatedly issue a receive method to receive data packets. To Run Run MultiExample2SenderReceiver on E: clientServerProgramming/multicast Run the file a few times to create a few participants Hit return when ready to say hello. 2018/7/5 Client-server programming


Download ppt "Block 14 Group Communication (Multicast)"

Similar presentations


Ads by Google