[an error occurred while processing this directive]

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:

               

                         

[an error occurred while processing this directive]