Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Multicast Client in Java 
 

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:

               

                         

» View all related tutorials
Related Tags: java c com server layout time button io graph screen command this disable pear cast tar app cas buttons start

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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:



Im new to java, i found this code very intresting espcially that im still trying to learn how to multicast ip packets.

can you PLEASE tell me how to run this code.
can i run it on just one laptop ? if yes can you tell me how?
i urgently need your help.thank you so much

Posted by Lizzie on Saturday, 12.22.07 @ 17:42pm | #43375

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex 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.