simple code for multicast chatting RRS feed

  • General discussion

  • hi guys ...

    this is asimple code for multicast chatting.....

    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Calendar;
     * Multicast based chat system with a very simple GUI using SWING
     * The main thread does the sending, while a secondary thread (the reader)
     * does the receiving of messages

    public class MultiChat extends JPanel implements ActionListener

        JTextArea textarea = null;
        JTextField textfield = null;
        JScrollPane scrollpane = null;
        private String id;
        private MulticastSocket socket = null;
        private SocketAddress dest = null;

         * Send the text to the multicast group.
         * Add the name of the user in front of the message.
        private void send(String msg) throws IOException
            String buf = id + ">> " + msg;
            byte[] b = buf.getBytes();
            DatagramPacket packet = new DatagramPacket(b, 0, b.length, dest);

         * Reader thread.
        class Reader extends Thread {
            public void run() {
                byte[] buf = new byte[512];
                DatagramPacket p = null;
                // Just loop forever, as the main thread will take care of the
                // exit conditions
                while (true) {
                    try {
                        p = new DatagramPacket(buf, buf.length);
                    } catch (IOException e) {
                    // We got a packet, let's extract the message and display it
                    String line = new String(p.getData(), 0, p.getLength());

         * Main constructor.
         * Takes a String containing the name (or id) of the user.
        MultiChat(String name) {

            id = name;

            // This is the multicast group we'll be using.
            dest = new InetSocketAddress("", 4321);

            try {
                // The socket is created and bound to the proper port.
                socket = new MulticastSocket(4321);
                // Set the loopback mode to false if you don't want to see your
                // own messages

                // get the default Network Interface
                NetworkInterface netif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
                // Join the multicast group
                socket.joinGroup(dest, netif);
            } catch (Exception e) {

            // From here onwards, the GUI is created

            // Force SwingSet to come up in the Cross Platform L&F
                // If you want the System L&F instead, comment out the above line and
                // uncomment the following:
                // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception exc) {
                System.err.println("Error loading L&F: " + exc);

            setLayout(new BorderLayout());

            // Create the area in which the messages will be seen after a RETURN is hit.
            // Using the JTextArea class to construct the GUI for this purpose.
            textarea = new JTextArea();
            scrollpane = new JScrollPane(textarea);

            // Create the area in which the user can key in the text message to be displayed.
            // Using the JTextField class to construct the GUI for this purpose.
            textfield = new JTextField();

            add("Center", scrollpane);
            add("South", textfield);

            Reader r = new Reader();

        // When the user hits <RETURN> this method is called
        public void actionPerformed(ActionEvent evt) {
            String text = textfield.getText();
            try {

                // So you want to quit?
                if ("quit".equalsIgnoreCase(text)) {
                    // Let's be nice

                // OK, let's just send the message then

                // And clear the TextField

            } catch (IOException e) {




         * To shutdown when run as an application.  This is a
         * fairly lame implementation.   A more self-respecting
         * implementation would at least check to see if a save
         * was needed.

        protected static final class AppCloser extends WindowAdapter {

            public void windowClosing(WindowEvent e) {


         * main() method of this program. The method creates a frame with Border Layout
         * and adds the MultiChat GUI onto the center of the frame with text string as
         * a label on the bottom of the frame to inform the user how to quit the application.

        public static void main(String[] args)  throws IOException

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String name;
            System.out.println("Enter your name :");
             if (args.length > 0)
                name = args[0];
            JFrame frame = new JFrame();
            frame.setTitle(name + "@ChatRoom");

            // Add the comps...
            frame.getContentPane().setLayout(new BorderLayout());
            frame.getContentPane().add("Center", new MultiChat(name));
            frame.getContentPane().add("South", new JLabel("Type \'quit\' to exit or just close the window"));
            frame.addWindowListener(new AppCloser());
            frame.setSize(400, 200);




    Friday, March 16, 2007 7:35 AM

All replies

  • thanks for the code... it has one deprecated method show() and one warning... while running we just see a window in which watever we type goes away the time we press enter... there is no client and server in the application.... can u explain wat actually yr code does....
    Friday, March 16, 2007 8:50 AM