Multicast Server in Java

This section introduces how to receive and send the IP packet by the multicast server and provides the functionality of multicast server through program.

Multicast Server in Java

Multicast Server in Java

     

This section introduces how to receive and send the IP packet by the multicast server and provides the functionality of multicast server through program. A brief description is given bellow. 

Description of program:

Here, a graphical layout appears on the screen when program runs that has two command buttons 'Start' and 'Stop'. First time 'Start' button is enable and another is disable. When you click 'Start' button, the multicast server functions but button is disable, and the message "Server is started" is displayed in text area. If you want to stop the server then click on the 'Stop' button then multicast server resume its function with a message that "Server is stopped". The multicast server receives requests or messages from multicast client, which has the port number '5000' and IP(Internate Protocol) number ('224.0.0.0', '235.0.0.1', '235.255.0.1', '224.0.255.1'). The server also sends same message to all multicast clients that are interested for this group otherwise don't perform these works. 

Description of code:

MulticastSocket(int port):
This is the constructor of MulticastSocket class. This class imports from import java.net.* package and extends the DatagramSocket that can be used for sending or receiving the multicast IP packets. It is same as DatagramSocket (UDP) and provides an extra facility to joining 'groups' of multicast hosts on the Internet. The above constructor constructs multicast socket. It takes integer type port number to bound it.

DatagramPacket(byte[] buf, int length, InetAddress address, int port):
DatagramPacket is a constructor of DatagramPacket class. This class extends the Object and imports form import java.net.* package that can be used for delivering the connectionless packet so packet delivering is not guarantee at the destination host. This constructor creates a datagram packet and delivers or transfers packet length to specified port number at the specified host. 

Here is the code of program:

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

public class UDPMulticastServer{
  JFrame frame;
  JPanel panel;
  JButton button1,button2;
  JTextArea area;
  JScrollPane pane;
  Thread thread;
  MulticastSocket socket;

  public static void main(String[] args) {
  UDPMulticastServer u = new UDPMulticastServer();
  }
  public UDPMulticastServer(){
  frame = new JFrame("Broadcast Server");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
 frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  panel = new JPanel();
  panel.setLayout(null);
  area = new JTextArea();
  button1 = new JButton("Start");
  button1.setBounds(210107540);
  button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  new StartServer();
  }
  });
  panel.add(button1);
  button2 = new JButton("Stop");
  button2.setBounds(300107540);
  button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  thread.interrupt();
  socket.close();
  area.append("Server is stopped\n");
  button1.setEnabled(true);
  button2.setEnabled(false);
  }
  });
  panel.add(button2);
  pane = new JScrollPane(area);
  pane.setBounds(1060365250);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(400400);
  frame.setVisible(true);
  }
  
  public class StartServer implements Runnable{
  InetAddress address;
  StartServer(){
  thread = new Thread(this);
  thread.start();
  button1.setEnabled(false);
  button2.setEnabled(true);
  }
  public void run(){
  try{
  byte[] buffer = new byte[65535];
  int port = 5000;
  String address = "224.0.0.0";
  String address1 = "235.0.0.1";
  String address2 = "235.255.0.1";
  String address3 = "224.0.255.1";
  String addresStr[] {address, address1, address2, address3};
  try{
  socket = new MulticastSocket(port);
  InetAddress add = InetAddress.getByName(address);
  socket.joinGroup(add);
  InetAddress add1 = InetAddress.getByName(address1);
  socket.joinGroup(add1);
  InetAddress add2 = InetAddress.getByName(address2);
  socket.joinGroup(add2);
  InetAddress add3 = InetAddress.getByName(address3);
  socket.joinGroup(add3);
  InetAddress str[] {add, add1, add2, add3};
  area.append("Server is started\n");
  while(true){
  try{
  //Receive request from client
  for(int i=0; i < str.length; i++){
  DatagramPacket packet = new DatagramPacket(buffer, buffer.length, str[i], port);
  socket.receive(packet);
  addresStr[i= packet.getAddress().toString();
  InetAddress client = packet.getAddress();
  int client_port = packet.getPort();
  area.append("Received : '" new String(buffer).trim() "' from " + addresStr[i"\n");
  // send information to the client
  String message = "your request\n ";
  buffer = message.getBytes() ;
  packet = new DatagramPacket(buffer, buffer.length, client, client_port);
  socket.send(packet);
  }
  }
  catch(UnknownHostException ue){}
  }
  }
  catch(java.net.BindException b){}
  }
  catch (IOException e){
  System.err.println(e);
  }
  }
  }
}

Download this example.

Output of program: