|
Network Programming This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Tutorial - Java Network Programming - Multicast Chat SystemOverview : In this tutorial, you compile, run, and test the peer-to-peer multicast chat system. You then study the code and some questions about Java multicast programming. Activity 1Compile, 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.
Activity 2Write a Java program called UDPPortScan.class that looks for UDP ports in use on the local host. You can make use of the DatagramSocket constructor to create a socket that listens on a specific port. A SocketException is thrown if the socket can't be created. This means that the port number you want to use is already occupied by another application running on the same machine.
Compile and then execute the UDPPortScan.class to see what UDP port numbers are already in use in your machine.
Activity 3
import java.net.InetAddress; import java.net.DatagramPacket; import java.net.MulticastSocket; import java.net.SocketException; import java.io.IOException; public class MulticastSniffer { public static void main(String[] args) { InetAddress ia = null; final byte[] buffer = new byte[65509]; final DatagramPacket dp = new DatagramPacket(buffer, buffer.length); int port = 0; // read the address from the command line try { ia = InetAddress.getByName (args[0]); port = Integer.parseInt (args[1]); } catch (Exception e) { System.err.println(e); System.err.println("Usage: java MulticastSniffer MulticastAddress port"); System.exit(1); } try { // missing code // missing code while (true) { // missing code final String s = new String(dp.getData(), 0, dp.getLength()); System.out.println(s); } } catch (SocketException se) { System.err.println(se); } catch (IOException ie) { System.err.println(ie); } } } 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 (); } } Questions for Discussion1. 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. In this peer-to-peer mode of a multicast chat system, can we prevent anyone from joining the multicast group? If so, how?
4. 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?
5. 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?
|