import java.awt.event.*; import com.ezjavabeans.ezmulticast.*; import javax.swing.*; /** This example shows how to create a simple chat with EzMulticast.*/ public class SimpleChat implements EzMulticastEventListener{ EzMulticast ezMulticast = new EzMulticast(); //Swing composants JFrame frame = new JFrame(); JPanel panel = new JPanel(); JLabel labelRcv = new JLabel(); JLabel labelMsg = new JLabel(); JTextField textRcv = new JTextField(); JTextField textMsg = new JTextField(); JButton buttonSend = new JButton(); //############################################################################ public static void main(String[] args) { SimpleChat sc = new SimpleChat(); sc.jbInit(); sc.frame.show(); }//main()--------------------------------------------------------------------- //############################################################################ /** Initialization of all the composants */ private void jbInit(){ try{ //Sets the Group Address for multicasting ezMulticast.setGroupAddress("230.0.0.1"); //Sets the Group port for multicasting ezMulticast.setGroupPort(4446); //Sets the Serial Key to activate the full version of EzMulticast. ezMulticast.setSerialKey("Sets the Serial Key to activate the full version."); //Registers to receive Data from EzMulticast ezMulticast.addEzMulticastEventListener(this); //Activates EzMulticast -> Ready to send and receive data ezMulticast.setActive(true); //Sets all the swing components. frame.setTitle("Simple Multicast Chat"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(null); frame.getContentPane().add(panel); labelRcv.setText("Message Received: "); labelMsg.setText("Send a Message: "); buttonSend.setText("Send!"); labelRcv.setBounds(10,10,170,20); labelMsg.setBounds(10,35,170,20); textRcv.setBounds(125,10,330,20); textRcv.setEditable(false); textMsg.setBounds(125,35,250,20); buttonSend.setBounds(375,35,80,20); panel.add(labelRcv); panel.add(labelMsg); panel.add(textRcv); panel.add(textMsg); panel.add(buttonSend); frame.setSize(470,90); frame.setResizable(false); buttonSend.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { try{ //Sends a message to the group ezMulticast.send(textMsg.getText().getBytes()); }catch(EzMulticastException ex){ System.out.println(ex.getMessage()); } } }); }catch(EzMulticastException ex){ System.out.println(ex.getMessage()); } }//jbInit()------------------------------------------------------------------- //############################################################################ /** Method called when EzMulticast receives a datagram */ public void dataIn(EzMulticastDataInEvent e){ String s = new String(e.data); textRcv.setText(s + " From : " + e.hostName); }//dataIn()------------------------------------------------------------------- }