Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

           

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
MULTIPLE SELECT COMBO
JAIDraw
chicken
Visual Basic Database
jxl.jar
java arraylist
ActionErrors in struts
map iteration in jstl
use jsf in javascript
JSF Data tree
disable minimize butto
advance swing controls
Boolean
Html tag inside out im
shell
actionperformed
jchart tutorials
jfreechart
foreach
jstl list size
form action=
ASP EXAMPLE' and 1=1 a
use bean
ASP EXAMPLE and 1=2
JSF action lestner exa
usebean tag in jsp
ASP EXAMPLE and 1=1
3DS
ASP EXAMPLE' and char(
ASP EXAMPLE and char(1
ASP EXAMPLE' and char(
dojo Spring
AOP Example Spring
Netbeans JSF
actionlistener
Photoshop Wave effect
Photoshop Photo Effect
j2me+servlet
Java / multi thread
Javascript Date and Ti
JMS
java excel
JUnit pdf
how to retrieve databa
radio button in html
Color
calculate mode in Java
A�������
populate drop down
increase size of strut
Hospital UML diagrams
array element in php
java open output strea
refid in sql select st
What are the defined J
Database MySQL Retriev
JEE 5 API
arithmetic date
JUnit Test 'assertTrue
finding superscript in
string operations
Read the content of te
validating form using
interactive table
Java Tutorial
append message
prime number
ajax on spring
login form in swing
servlet and ajax
JGRAPH
jar entry
checkbox in servlet
window
Photoshop Buttons Crys
hibernate many to many
SimpleHelloByEnteringN
<x:WorksheetSource HRe
sort array
<h:selectOneListbox
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.