Recent Changes - Search:

Distributed Computing

This website demonstrates using wikis as teaching and learning tool.

The course instructor is also happy to share the teaching materials here with those who find it readable.

Tutorial - Multicast Chat System

A Distributed Computing Tutorial by Steven Choy

Activity

Compile, run, and test the peer-to-peer multicast chat system according to the following steps:
1. Compile the source code MulticastChat.java to generate MulticastChat.class:
javac MulticastChat.java
2. Then launch the program several times to connect to a multicast group, for example,
java MulticastChat 239.1.2.3:1234
3. After that, test the chat system by typing text into the input region of different frames to see whether they work as expected.

Questions

1. Can you create a MulticastSocket that listens and transmits on a UDP port that has been used by a DatagramSocket sitting in the same machine?
2. Is it possible to create a MulticastSocket that listens and transmits on a UDP port that has been used by other MulticastSocket sitting in the same machine?.
3. List the seven methods that add to MulticastSocket on top of DatagramSocket.
4. Which types of exception may all the methods of MulticastSocket throw?
5. In this peer-to-peer mode of a multicast chat system, can we prevent anyone from joining the multicast group? If so, how?
6. Can we modify the program so that it is able to accept UDP packets only from the pre-specified range of IP addresses? If so, how?
7. If somebody in the network knows the multicast group and the UDP port number being used by the multicast chat system, is it possible for him or her to create annoying effects, or even crash our system?

Java Code: Multicast Chat

/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class MulticastChat implements Runnable, WindowListener, ActionListener {
  protected InetAddress group;
  protected int port;

  public MulticastChat (InetAddress group, int port) {
    this.group = group;
    this.port = port;
    initAWT ();
  }

  protected Frame frame;
  protected TextArea output;
  protected TextField input;

  protected void initAWT () {
    frame = new Frame
      ("MulticastChat [" + group.getHostAddress () + ":" + port + "]");
    frame.addWindowListener (this);
    output = new TextArea ();
    output.setEditable (false);
    input = new TextField ();
    input.addActionListener (this);
    frame.setLayout (new BorderLayout ());
    frame.add (output, "Center");
    frame.add (input, "South");
    frame.pack ();
  }

  protected Thread listener;

  public synchronized void start () throws IOException {
    if (listener == null) {
      initNet ();
      listener = new Thread (this);
      listener.start ();
      frame.setVisible (true);
    }
  }

  protected MulticastSocket socket;
  protected DatagramPacket outgoing, incoming;

  protected void initNet () throws IOException {
    socket = new MulticastSocket (port);
    socket.setTimeToLive (5);
    socket.joinGroup (group);
    outgoing = new DatagramPacket (new byte[1], 1, group, port);
    incoming = new DatagramPacket (new byte[65508], 65508);
  }

  public synchronized void stop () throws IOException {
    frame.setVisible (false);
    if (listener != null) {
      listener.interrupt ();
      listener = null;
      try {
        socket.leaveGroup (group);
      } finally {
        socket.close ();
      }
    }
  }

  public void windowOpened (WindowEvent event) {
    input.requestFocus ();
  }

  public void windowClosing (WindowEvent event) {
    try {
      stop ();
    } catch (IOException ex) {
      ex.printStackTrace ();
    }
  }

  public void windowClosed (WindowEvent event) {}
  public void windowIconified (WindowEvent event) {}
  public void windowDeiconified (WindowEvent event) {}
  public void windowActivated (WindowEvent event) {}
  public void windowDeactivated (WindowEvent event) {}

  public void actionPerformed (ActionEvent event) {
    try {
      byte[] utf = event.getActionCommand ().getBytes ("UTF8");
      outgoing.setData (utf);
      outgoing.setLength (utf.length);
      socket.send (outgoing);
      input.setText ("");
    } catch (IOException ex) {
      handleIOException (ex);
    }
  }

  protected synchronized void handleIOException (IOException ex) {
    if (listener != null) {
      output.append (ex + "\n");
      input.setVisible (false);
      frame.validate ();
      if (listener != Thread.currentThread ())
        listener.interrupt ();
      listener = null;
      try {
        socket.leaveGroup (group);
      } catch (IOException ignored) {
      }
      socket.close ();
    }
  }

  public void run () {
    try {
      while (!Thread.interrupted ()) {
        incoming.setLength (incoming.getData ().length);
        socket.receive (incoming);
        String message = new String
          (incoming.getData (), 0, incoming.getLength (), "UTF8");
        output.append (message + "\n");
      }
    } catch (IOException ex) {
      handleIOException (ex);
    }
  }

  public static void main (String[] args) throws IOException {
    if ((args.length != 1) || (args[0].indexOf (":") < 0))
      throw new IllegalArgumentException
        ("Syntax: MulticastChat <group>:<port>");

    int idx = args[0].indexOf (":");
      InetAddress group = InetAddress.getByName (args[0].substring (0, idx));
    int port = Integer.parseInt (args[0].substring (idx + 1));

    MulticastChat chat = new MulticastChat (group, port);
    chat.start ();
  }
}

Suggested answers are available here.

Edit - History - Print - Recent Changes - Search
Page last modified on November 28, 2008, at 04:27 PM