www.pudn.com > jnp-src.rar > GenericChat.java


/*
 * 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.awt.*; 
import java.awt.event.*; 
 
public class GenericChat { 
  protected String host, name; 
  protected int port; 
   
  public GenericChat (String host, int port, String name) { 
    this.host = host; 
    this.port = port; 
    this.name = name; 
    initAWT (); 
  } 
 
  protected Frame frame; 
  protected ChatboardClient cb; 
  protected WhiteboardClient wb; 
   
  protected void initAWT () { 
    frame = new Frame ("Generic Chat"); 
    frame.setLayout (new GridLayout (2, 1)); 
    cb = new ChatboardClient (); 
    wb = new WhiteboardClient (); 
    frame.add (wb); 
    frame.add (cb); 
    frame.pack (); 
    frame.addWindowListener (new WindowAdapter () { 
      public void windowClosing (WindowEvent event) { 
        try { 
          stop (); 
        } catch (IOException ex) { 
          ex.printStackTrace (); 
        } 
      } 
    }); 
  } 
 
  protected GenericClient client; 
   
  public void start () throws IOException { 
    client = new GenericClient (host, port, name); 
    client.register ("chat", cb); 
    client.register ("wb", wb); 
    client.start (); 
    frame.setVisible (true); 
  } 
 
  public void stop () throws IOException { 
    frame.setVisible (false); 
    client.shutdown (); 
  } 
 
  public static void main (String[] args) throws IOException { 
    if (args.length != 3) 
      throw new IllegalArgumentException 
        ("Syntax: GenericChat   "); 
    GenericChat chat = new GenericChat 
      (args[0], Integer.parseInt (args[1]), args[2]); 
    chat.start (); 
  } 
}