Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Net Udp Multicast Client in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Multicast Client in Java

Advertisement
This section describes the ways to send and receive the IP packet or message by multicast client.

UDP Multicast Client in Java

     

This section describes the ways to send and receive the IP packet or message by multicast client. Here, we provide many multicast clients and it's functionality for sending or receiving messages at a time. The detail information provides bellow:

Description of program: 

Program runs then it provides a graphical layout that has four clients identified by the specific IP('224.0.0.0', '235.0.0.1', '235.255.0.1', '224.0.255.1') and port number(5000). Those of any client sends and receives IP packet that depends upon the check box. If check box is enable then send or receive IP packets otherwise it couldn't be send or receive. Just bellow provides a text area that can be used for writing the message and receiving the message to multicast server. It has also 'Send' command button for sending IP packet to multicast server. 

Here is the code of program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;  

public class UDPMulticastClient{
  JFrame frame;
  JPanel panel;
  JTextField field1, field2, field3, field4, 
field5, field6, field7, field8;

  JTextArea area;
  JScrollPane pane;
  JLabel label;
  JButton button;
  JList list;
  Checkbox check1, check2, check3, check4;
  public static void main(String[] args) {
  UDPMulticastClient u = new UDPMulticastClient();
  }
  public UDPMulticastClient(){
  frame = new JFrame("UDP Broadcast Client");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  panel = new JPanel();
  panel.setLayout(null);
  label = new JLabel("Destination IP");
  label.setBounds(80510030);
  panel.add(label);
  check1 = new Checkbox();
  check1.setBounds(5402020);
  panel.add(check1);
  label = new JLabel("Client 1 :");
  label.setBounds(25356030);
  panel.add(label);
  field1 = new JTextField(20);
  field1.setBounds(803512020);
  panel.add(field1);
  label = new JLabel("Destination Port");
  label.setBounds(255510030);
  panel.add(label);
  field2 = new JTextField(10);
  field2.setBounds(2553510020);
  panel.add(field2);
  check2 = new Checkbox();
  check2.setBounds(5702020);
  panel.add(check2);
  label = new JLabel("Client 2 :");
  label.setBounds(25656030);
  panel.add(label);
  field3 = new JTextField(20);
  field3.setBounds(806512020);
  panel.add(field3);
  field4 = new JTextField(10);
  field4.setBounds(2556510020);
  panel.add(field4);
  check3 = new Checkbox();
  check3.setBounds(51002020);
  panel.add(check3);
  label = new JLabel("Client 3 :");
  label.setBounds(25956030);
  panel.add(label);
  field5 = new JTextField(20);
  field5.setBounds(809512020);
  panel.add(field5);
  field6 = new JTextField(10);
  field6.setBounds(2559510020);
  panel.add(field6);
  check4 = new Checkbox();
  check4.setBounds(51302020);
  panel.add(check4);
  label = new JLabel("Client 4 :");
  label.setBounds(251256030);
  panel.add(label);
  field7 = new JTextField(20);
  field7.setBounds(8012512020);
  panel.add(field7);
  field8 = new JTextField(10);
  field8.setBounds(25512510020);
  panel.add(field8);
  label = new JLabel("Message:");
  label.setBounds(101608030);
  panel.add(label);
  area = new JTextArea();
  pane = new JScrollPane(area);
  pane.setBounds(10190300200);
  panel.add(pane);
  button = new JButton("Send");
  button.setBounds(2354107530);
  button.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  new SendRequest();
  }
  });
  panel.add(button);
  frame.add(panel);
  frame.setSize(400500);
  frame.setVisible(true);
  }
  public class SendRequest{
  SendRequest(){
  try{
  boolean b = true;
  if(check1.getState() == b){
  String dip = field1.getText();
  InetAddress address = InetAddress.getByName(dip);
  MulticastSocket socket = new MulticastSocket();
  socket.joinGroup(address);
  String port = field2.getText();
  int pnum = Integer.parseInt(port);
  String mess = area.getText();
  byte message[] = mess.getBytes();
  DatagramPacket packet = new DatagramPacket

(
message, message.length, address, pnum);
  socket.send(packet);
  area.setText("");
  //For Received message
  DatagramPacket packet1 = new DatagramPacket

(
message, message.length);
  socket.receive(packet1);
  String recmessage =new String(packet1.getData());
  area.append("Received from server: " + recmessage);
  socket.close();
  }
  if(check2.getState() == b){
  String dip = field3.getText();
  InetAddress address = InetAddress.getByName(dip);
  MulticastSocket socket = new MulticastSocket();
  socket.joinGroup(address);
  String port = field4.getText();
  int pnum = Integer.parseInt(port);
  String mess = area.getText();
  byte message[] = mess.getBytes();
  DatagramPacket packet = new DatagramPacket(message, message.length, address, pnum);
  socket.send(packet);
  area.setText("");
  //For Received message
  DatagramPacket packet1 = new DatagramPacket(message, message.length);
  socket.receive(packet1);
  String recmessage =new String(packet1.getData());
  area.append("Received from server: " + recmessage);
  socket.close();
  }
  if(check3.getState() == b){
  String dip = field5.getText();
  InetAddress address = InetAddress.getByName(dip);
  MulticastSocket socket = new MulticastSocket();
  socket.joinGroup(address);
  String port = field6.getText();
  int pnum = Integer.parseInt(port);
  String mess = area.getText();
  byte message[] = mess.getBytes();
  DatagramPacket packet = new DatagramPacket

(
message, message.length, address, pnum);
  socket.send(packet);
  area.setText("");
  //For Received message
  DatagramPacket packet1 = new DatagramPacket(message, message.length);
  socket.receive(packet1);
  String recmessage =new String(packet1.getData());
  area.append("Received from server: " + recmessage);
  socket.close();
  }
  if(check4.getState() == b){
  String dip = field7.getText();
  InetAddress address = InetAddress.getByName(dip);
  MulticastSocket socket = new MulticastSocket();
  socket.joinGroup(address);
  String port = field8.getText();
  int pnum = Integer.parseInt(port);
  String mess = area.getText();
  byte message[] = mess.getBytes();
  DatagramPacket packet = new DatagramPacket

(
message, message.length, address, pnum);
  socket.send(packet);
  area.setText("");
  //For Received message
  DatagramPacket packet1 = new DatagramPacket(message, message.length);
  socket.receive(packet1);
  String recmessage =new String(packet1.getData());
  area.append("Received from server: " + recmessage);
  socket.close();
  }
  }
  catch(IOException io){}
  }
  }
}

Download this example.

Output of program:

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: Multicast Client in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
alakhrass
May 24, 2011
Project: Multicast under UDP: Reliability

Project: Multicast under UDP: Reliability 12/11/10 UDP is used to support mulicast. Recall that UDP is connectionless and non reliable. Hence, there is no guarantee that a multicast message will be received by all a group hosts. The aim of this project is to implement a solution that provides reliability for multicast applications under UDP. For more information, check the following site which provides different solutions to multicast reliability: http://datatracker.ietf.org/wg/rmt/charter/ In this project, you will write two classes RMulticastServer (called server in the following) and RMulticastClient (called client in the following) in Java that implement a simple solution to provide reliability in multicast. You have to allow up to 10 RMulticastClient to have the possibility of parallel subscribing to the multicast group address that your RMulticastServer will serve. This server has to keep tracks of all the clients that have subscribed. For each message multicasted by the server, the latter should receive an acknowledgment sent by the clients that have subscribed. If not all the acknowledgments (from all clients) have been received by the server during a timeout period, the server will send the message in unicast to all the clients that have subscribed but which fail to send an acknowldgement. The server will not serve the next multicast message until all acknowledgment are received In particular, the server has to provide the following: 1. The server and clients have to allow the user to indicate the multicast group address and the port number. In addition, the clients have to make it possible to a user to indicate which server he will use for subscription 2. The server has to ask the user to provide the number of messages that the server will multicast. In addition, the server will continue to listen the requests from clients (join and leave requests) until all messages from the user are entered. 3. You have to simulate message loss: this achieved by letting the server and clients accept only ninety percent (90%) of the messages that have been received: a. The server and clients discard 10% of the messages b. The acknowledgment of the clients will be sent only for the messages that haves been accepted (ie those messages that have been not discarded). c. The server will discard 10% of acknowledgements 4. The server will use a timeout of five seconds, that is, if an acknowledgement from some clients does not come before the expiration of this timeout, the server will retransmit, in unicast, the message that have not been acknowledged before the timer expiration to all the concerned clients (ie the clients that have not succeed to sent an acknowledgement or if the acknowledgement has been discarded as mentioned in step 3 above). 5. The content of the sent message has to be displayed by the server: a. A message has been sent as multicast or as a re-transmission and in this latter case you have to provide the destination of the message 6. After a message has been accepted by a client, the content of the message has to be displayed. In addition the client has to specify if the message is multicast or unicast (re-transmission).
maher
March 29, 2012
urgent

hi, where is the solution of this Disscus because I want to know what is the code of this program .. plz reply and send to me the link of solution .. thx alot
DMCA.com