import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; /** * UMLV Multicast Chat client v0.1 * @author Daniele Raffo, 9 JAN 2002 */ public class UMLVMulticastChat implements Runnable, ActionListener { static final int CHATW_LINES = 500; static final String DEFAULT_MADDRESS = "228.45.56.1"; static final int DEFAULT_PORT = 1777; static final int MAX_BUFSIZE = 1024; String username; InetAddress mAddress; int port; MulticastSocket m; JLabel title; JTextField msgline; JTextArea msgarea; int nlines = 0; /** Constructor. Builds the GUI for the application. */ public UMLVMulticastChat() throws IOException { username = System.getProperty("user.name"); JFrame frame = new JFrame("UMLV Multicast Chat"); JPanel panel = new JPanel(new BorderLayout()); panel.setPreferredSize(new Dimension(650, 500)); title = new JLabel(); panel.add(title, BorderLayout.NORTH); msgline = new JTextField(); panel.add(msgline, BorderLayout.SOUTH); msgarea = new JTextArea(); msgarea.setEditable(false); msgarea.setLineWrap(true); msgarea.setWrapStyleWord(true); JScrollPane scroll = new JScrollPane(msgarea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); panel.add(scroll, BorderLayout.CENTER); Font font = new Font("Serif", Font.ITALIC, 16); title.setFont(font); msgline.setFont(font); msgarea.setFont(font); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { say("*** " + username + " disconnected ***"); System.exit(0); } }); msgline.addActionListener(this); msgline.requestFocus(); } /** Main method. */ public static void main (String[] args) throws Exception { System.out.println("UMLV Multicast Chat client v0.1 9 JAN 2002 \n" + "by Daniele Raffo "); if ((args.length != 0) && (args.length != 2)) { System.out.println( "Usage: java UMLVMulticastChat [ ]"); System.exit(-1); } UMLVMulticastChat chat = new UMLVMulticastChat(); if (args.length == 0) { chat.mAddress = InetAddress.getByName(DEFAULT_MADDRESS); chat.port = DEFAULT_PORT; } else { chat.mAddress = InetAddress.getByName(args[0]); chat.port = Integer.parseInt(args[1]); } // creates the multicast socket and connects to the multicast address chat.m = new MulticastSocket(chat.port); chat.m.joinGroup(chat.mAddress); chat.m.setTimeToLive(32); chat.title.setText("Multicast on " + chat.mAddress.getHostAddress() + ", port " + chat.port); chat.say("*** " + chat.username + " connected from " + InetAddress.getLocalHost().getHostName() + " (" + InetAddress.getLocalHost().getHostAddress() + ") ***"); // creates a new thread for the reception of messages Thread t = new Thread(chat); t.setDaemon(true); t.start(); } /** Gets the message typed by the user and sends it to multicast. */ public void actionPerformed(ActionEvent event) { say("<" + username + "> " + msgline.getText()); msgline.setText(""); } /** Sends a text to multicast. */ public void say(String text) { byte[] data = new byte[MAX_BUFSIZE]; DatagramPacket packet = new DatagramPacket(data, 0, 0, mAddress, port); try { data = text.getBytes(); packet.setData(data, 0, data.length); m.send(packet); } catch (IOException e) { System.out.println(e); } } /** Waits for messages to arrive from multicast and prints them. */ public void run() { byte[] data = new byte[MAX_BUFSIZE]; DatagramPacket packet = new DatagramPacket(data, 0, data.length); try { while (true) { packet.setData(data, 0, data.length); m.receive(packet); if (nlines >= CHATW_LINES) { msgarea.setText(null); nlines = 0; } msgarea.append(new String(data, 0, packet.getLength()) + System.getProperty("line.separator")); Document d = msgarea.getDocument(); msgarea.select(d.getLength(), d.getLength()); nlines++; } } catch (IOException e) { System.out.println(e); } } }