<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FTiny_Application%2FChat</id>
		<title>Java/Tiny Application/Chat - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FTiny_Application%2FChat"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Tiny_Application/Chat&amp;action=history"/>
		<updated>2026-04-21T22:09:44Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Tiny_Application/Chat&amp;diff=8831&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Tiny_Application/Chat&amp;diff=8831&amp;oldid=prev"/>
				<updated>2010-06-01T07:19:40Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:19, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Tiny_Application/Chat&amp;diff=8830&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Tiny_Application/Chat&amp;diff=8830&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:47Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Chat client server ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
/**&lt;br /&gt;
 * ChatClient.java  1.00 96/11/07 Merlin Hughes&lt;br /&gt;
 *&lt;br /&gt;
 * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Permission to use, copy, modify, and distribute this software&lt;br /&gt;
 * for non-commercial purposes and without fee is hereby granted&lt;br /&gt;
 * provided that this copyright notice appears in all copies.&lt;br /&gt;
 *&lt;br /&gt;
 * http://prominence.ru/                  merlin@prominence.ru&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.awt.*;&lt;br /&gt;
public class ChatClient extends Frame implements Runnable {&lt;br /&gt;
  protected DataInputStream i;&lt;br /&gt;
  protected DataOutputStream o;&lt;br /&gt;
  protected TextArea output;&lt;br /&gt;
  protected TextField input;&lt;br /&gt;
  protected Thread listener;&lt;br /&gt;
  public ChatClient (String title, InputStream i, OutputStream o) {&lt;br /&gt;
    super (title);&lt;br /&gt;
    this.i = new DataInputStream (new BufferedInputStream (i));&lt;br /&gt;
    this.o = new DataOutputStream (new BufferedOutputStream (o));&lt;br /&gt;
    setLayout (new BorderLayout ());&lt;br /&gt;
    add (&amp;quot;Center&amp;quot;, output = new TextArea ());&lt;br /&gt;
    output.setEditable (false);&lt;br /&gt;
    add (&amp;quot;South&amp;quot;, input = new TextField ());&lt;br /&gt;
    pack ();&lt;br /&gt;
    show ();&lt;br /&gt;
    input.requestFocus ();&lt;br /&gt;
    listener = new Thread (this);&lt;br /&gt;
    listener.start ();&lt;br /&gt;
  }&lt;br /&gt;
  public void run () {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        String line = i.readUTF ();&lt;br /&gt;
        output.appendText (line + &amp;quot;\n&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException ex) {&lt;br /&gt;
      ex.printStackTrace ();&lt;br /&gt;
    } finally {&lt;br /&gt;
      listener = null;&lt;br /&gt;
      input.hide ();&lt;br /&gt;
      validate ();&lt;br /&gt;
      try {&lt;br /&gt;
        o.close ();&lt;br /&gt;
      } catch (IOException ex) {&lt;br /&gt;
        ex.printStackTrace ();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public boolean handleEvent (Event e) {&lt;br /&gt;
    if ((e.target == input) &amp;amp;&amp;amp; (e.id == Event.ACTION_EVENT)) {&lt;br /&gt;
      try {&lt;br /&gt;
        o.writeUTF ((String) e.arg);&lt;br /&gt;
        o.flush ();&lt;br /&gt;
      } catch (IOException ex) {&lt;br /&gt;
        ex.printStackTrace();&lt;br /&gt;
        listener.stop ();&lt;br /&gt;
      }&lt;br /&gt;
      input.setText (&amp;quot;&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    } else if ((e.target == this) &amp;amp;&amp;amp; (e.id == Event.WINDOW_DESTROY)) {&lt;br /&gt;
      if (listener != null)&lt;br /&gt;
        listener.stop ();&lt;br /&gt;
      hide ();&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    return super.handleEvent (e);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main (String args[]) throws IOException {&lt;br /&gt;
    if (args.length != 2)&lt;br /&gt;
      throw new RuntimeException (&amp;quot;Syntax: ChatClient &amp;lt;host&amp;gt; &amp;lt;port&amp;gt;&amp;quot;);&lt;br /&gt;
    Socket s = new Socket (args[0], Integer.parseInt (args[1]));&lt;br /&gt;
    new ChatClient (&amp;quot;Chat &amp;quot; + args[0] + &amp;quot;:&amp;quot; + args[1],&lt;br /&gt;
                    s.getInputStream (), s.getOutputStream ());&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
////////&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * ChatApplet.java  1.00 96/11/01 Merlin Hughes&lt;br /&gt;
 *&lt;br /&gt;
 * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Permission to use, copy, modify, and distribute this software&lt;br /&gt;
 * for non-commercial purposes and without fee is hereby granted&lt;br /&gt;
 * provided that this copyright notice appears in all copies.&lt;br /&gt;
 *&lt;br /&gt;
 * http://prominence.ru/                  merlin@prominence.ru&lt;br /&gt;
 */&lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.awt.*;&lt;br /&gt;
import java.applet.*;&lt;br /&gt;
// Applet parameters:&lt;br /&gt;
//   host = host name&lt;br /&gt;
//   port = host port&lt;br /&gt;
public class ChatApplet extends Applet implements Runnable {&lt;br /&gt;
  protected DataInputStream i;&lt;br /&gt;
  protected DataOutputStream o;&lt;br /&gt;
  protected TextArea output;&lt;br /&gt;
  protected TextField input;&lt;br /&gt;
  protected Thread listener;&lt;br /&gt;
  public void init () {&lt;br /&gt;
    setLayout (new BorderLayout ());&lt;br /&gt;
    add (&amp;quot;Center&amp;quot;, output = new TextArea ());&lt;br /&gt;
    output.setEditable (false);&lt;br /&gt;
    add (&amp;quot;South&amp;quot;, input = new TextField ());&lt;br /&gt;
    input.setEditable (false);&lt;br /&gt;
  }&lt;br /&gt;
  public void start () {&lt;br /&gt;
    listener = new Thread (this);&lt;br /&gt;
    listener.start ();&lt;br /&gt;
  }&lt;br /&gt;
  public void stop () {&lt;br /&gt;
    if (listener != null)&lt;br /&gt;
      listener.stop ();&lt;br /&gt;
    listener = null;&lt;br /&gt;
  }&lt;br /&gt;
  public void run () {&lt;br /&gt;
    try {&lt;br /&gt;
      String host = getParameter (&amp;quot;host&amp;quot;);&lt;br /&gt;
      if (host == null)&lt;br /&gt;
  host = getCodeBase ().getHost ();&lt;br /&gt;
      String port = getParameter (&amp;quot;port&amp;quot;);&lt;br /&gt;
      if (port == null)&lt;br /&gt;
  port = &amp;quot;9830&amp;quot;;&lt;br /&gt;
      output.appendText (&amp;quot;Connecting to &amp;quot; + host + &amp;quot;:&amp;quot; + port + &amp;quot;...&amp;quot;);&lt;br /&gt;
      Socket s = new Socket (host, Integer.parseInt (port));&lt;br /&gt;
      i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));&lt;br /&gt;
      o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));&lt;br /&gt;
      output.appendText (&amp;quot; connected.\n&amp;quot;);&lt;br /&gt;
      input.setEditable (true);&lt;br /&gt;
      input.requestFocus ();&lt;br /&gt;
      execute ();&lt;br /&gt;
    } catch (IOException ex) {&lt;br /&gt;
      ByteArrayOutputStream out = new ByteArrayOutputStream ();&lt;br /&gt;
      ex.printStackTrace (new PrintStream (out));&lt;br /&gt;
      output.appendText (&amp;quot;\n&amp;quot; + out);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void execute () {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        String line = i.readUTF ();&lt;br /&gt;
        output.appendText (line + &amp;quot;\n&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException ex) {&lt;br /&gt;
      ByteArrayOutputStream out = new ByteArrayOutputStream ();&lt;br /&gt;
      ex.printStackTrace (new PrintStream (out));&lt;br /&gt;
      output.appendText (out.toString ());&lt;br /&gt;
    } finally {&lt;br /&gt;
      listener = null;&lt;br /&gt;
      input.hide ();&lt;br /&gt;
      validate ();&lt;br /&gt;
      try {&lt;br /&gt;
        o.close ();&lt;br /&gt;
      } catch (IOException ex) {&lt;br /&gt;
        ex.printStackTrace ();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public boolean handleEvent (Event e) {&lt;br /&gt;
    if ((e.target == input) &amp;amp;&amp;amp; (e.id == Event.ACTION_EVENT)) {&lt;br /&gt;
      try {&lt;br /&gt;
        o.writeUTF ((String) e.arg);&lt;br /&gt;
        o.flush ();&lt;br /&gt;
      } catch (IOException ex) {&lt;br /&gt;
        ex.printStackTrace();&lt;br /&gt;
        listener.stop ();&lt;br /&gt;
      }&lt;br /&gt;
      input.setText (&amp;quot;&amp;quot;);&lt;br /&gt;
      return true;&lt;br /&gt;
    } else if ((e.target == this) &amp;amp;&amp;amp; (e.id == Event.WINDOW_DESTROY)) {&lt;br /&gt;
      if (listener != null)&lt;br /&gt;
        listener.stop ();&lt;br /&gt;
      hide ();&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    return super.handleEvent (e);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
///////////&lt;br /&gt;
/**&lt;br /&gt;
 * ChatHandler.java  1.00 96/11/07 Merlin Hughes&lt;br /&gt;
 *&lt;br /&gt;
 * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Permission to use, copy, modify, and distribute this software&lt;br /&gt;
 * for non-commercial purposes and without fee is hereby granted&lt;br /&gt;
 * provided that this copyright notice appears in all copies.&lt;br /&gt;
 *&lt;br /&gt;
 * http://prominence.ru/                  merlin@prominence.ru&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
public class ChatHandler extends Thread {&lt;br /&gt;
  protected Socket s;&lt;br /&gt;
  protected DataInputStream i;&lt;br /&gt;
  protected DataOutputStream o;&lt;br /&gt;
  public ChatHandler (Socket s) throws IOException {&lt;br /&gt;
    this.s = s;&lt;br /&gt;
    i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));&lt;br /&gt;
    o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));&lt;br /&gt;
  }&lt;br /&gt;
  protected static Vector handlers = new Vector ();&lt;br /&gt;
  public void run () {&lt;br /&gt;
    String name = s.getInetAddress ().toString ();&lt;br /&gt;
    try {&lt;br /&gt;
      broadcast (name + &amp;quot; has joined.&amp;quot;);&lt;br /&gt;
      handlers.addElement (this);&lt;br /&gt;
      while (true) {&lt;br /&gt;
        String msg = i.readUTF ();&lt;br /&gt;
        broadcast (name + &amp;quot; - &amp;quot; + msg);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException ex) {&lt;br /&gt;
      ex.printStackTrace ();&lt;br /&gt;
    } finally {&lt;br /&gt;
      handlers.removeElement (this);&lt;br /&gt;
      broadcast (name + &amp;quot; has left.&amp;quot;);&lt;br /&gt;
      try {&lt;br /&gt;
        s.close ();&lt;br /&gt;
      } catch (IOException ex) {&lt;br /&gt;
        ex.printStackTrace();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  protected static void broadcast (String message) {&lt;br /&gt;
    synchronized (handlers) {&lt;br /&gt;
      Enumeration e = handlers.elements ();&lt;br /&gt;
      while (e.hasMoreElements ()) {&lt;br /&gt;
        ChatHandler c = (ChatHandler) e.nextElement ();&lt;br /&gt;
        try {&lt;br /&gt;
          synchronized (c.o) {&lt;br /&gt;
            c.o.writeUTF (message);&lt;br /&gt;
          }&lt;br /&gt;
          c.o.flush ();&lt;br /&gt;
        } catch (IOException ex) {&lt;br /&gt;
          c.stop ();&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
///////////&lt;br /&gt;
/**&lt;br /&gt;
 * ChatServer.java  1.00 96/11/07 Merlin Hughes&lt;br /&gt;
 *&lt;br /&gt;
 * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Permission to use, copy, modify, and distribute this software&lt;br /&gt;
 * for non-commercial purposes and without fee is hereby granted&lt;br /&gt;
 * provided that this copyright notice appears in all copies.&lt;br /&gt;
 *&lt;br /&gt;
 * http://prominence.ru/                  merlin@prominence.ru&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
public class ChatServer {&lt;br /&gt;
  public ChatServer (int port) throws IOException {&lt;br /&gt;
    ServerSocket server = new ServerSocket (port);&lt;br /&gt;
    while (true) {&lt;br /&gt;
      Socket client = server.accept ();&lt;br /&gt;
      System.out.println (&amp;quot;Accepted from &amp;quot; + client.getInetAddress ());&lt;br /&gt;
      ChatHandler c = new ChatHandler (client);&lt;br /&gt;
      c.start ();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void main (String args[]) throws IOException {&lt;br /&gt;
    if (args.length != 1)&lt;br /&gt;
      throw new RuntimeException (&amp;quot;Syntax: ChatServer &amp;lt;port&amp;gt;&amp;quot;);&lt;br /&gt;
    new ChatServer (Integer.parseInt (args[0]));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Simple console-mode (command-line) chat client ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.&lt;br /&gt;
 * All rights reserved. Software written by Ian F. Darwin and others.&lt;br /&gt;
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *    documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS&amp;quot;&amp;quot;&lt;br /&gt;
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED&lt;br /&gt;
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS&lt;br /&gt;
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR&lt;br /&gt;
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF&lt;br /&gt;
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS&lt;br /&gt;
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN&lt;br /&gt;
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)&lt;br /&gt;
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE&lt;br /&gt;
 * POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 * &lt;br /&gt;
 * Java, the Duke mascot, and all variants of Sun&amp;quot;s Java &amp;quot;steaming coffee&lt;br /&gt;
 * cup&amp;quot; logo are trademarks of Sun Microsystems. Sun&amp;quot;s, and James Gosling&amp;quot;s,&lt;br /&gt;
 * pioneering role in inventing and promulgating (and standardizing) the Java &lt;br /&gt;
 * language and environment is gratefully acknowledged.&lt;br /&gt;
 * &lt;br /&gt;
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&amp;amp;T, for&lt;br /&gt;
 * inventing predecessor languages C and C++ is also gratefully acknowledged.&lt;br /&gt;
 */&lt;br /&gt;
import java.applet.*;&lt;br /&gt;
import java.awt.*;&lt;br /&gt;
import java.awt.event.*;&lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import java.net.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
/** Simple console-mode (command-line) chat client.&lt;br /&gt;
 * @author Ian Darwin, http://www.darwinsys.ru/&lt;br /&gt;
 * @version $Id: ConsChat.java,v 1.6 2004/02/16 02:44:43 ian Exp $&lt;br /&gt;
 */&lt;br /&gt;
public class ConsChat {&lt;br /&gt;
  public static void main(String[] args) throws IOException {&lt;br /&gt;
    new ConsChat().chat();&lt;br /&gt;
  }&lt;br /&gt;
  protected Socket sock;&lt;br /&gt;
  protected BufferedReader is;&lt;br /&gt;
  protected PrintWriter pw;&lt;br /&gt;
  protected BufferedReader cons;&lt;br /&gt;
  protected ConsChat() throws IOException {&lt;br /&gt;
    sock = new Socket(&amp;quot;localhost&amp;quot;, Chat.PORTNUM);&lt;br /&gt;
    is   = new BufferedReader(new InputStreamReader(sock.getInputStream()));&lt;br /&gt;
    pw   = new PrintWriter(sock.getOutputStream(), true);&lt;br /&gt;
    cons = new BufferedReader(new InputStreamReader(System.in));&lt;br /&gt;
    // Construct and start the reader: from server to stdout.&lt;br /&gt;
    // Make a Thread to avoid lockups.&lt;br /&gt;
    new Thread() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        setName(&amp;quot;socket reader thread&amp;quot;);&lt;br /&gt;
        System.out.println(&amp;quot;Starting &amp;quot; + getName());&lt;br /&gt;
        System.out.flush();&lt;br /&gt;
        String line;&lt;br /&gt;
        try {&lt;br /&gt;
          // reader thread blocks here&lt;br /&gt;
          while ((line = is.readLine()) != null) {&lt;br /&gt;
            System.out.println(line);&lt;br /&gt;
            System.out.flush();&lt;br /&gt;
          }&lt;br /&gt;
        } catch (IOException ex) {&lt;br /&gt;
          System.err.println(&amp;quot;Read error on socket: &amp;quot; + ex);&lt;br /&gt;
          return;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }.start();&lt;br /&gt;
  }&lt;br /&gt;
  protected void chat() throws IOException {&lt;br /&gt;
    String text;&lt;br /&gt;
    System.out.print(&amp;quot;Login name: &amp;quot;); System.out.flush();&lt;br /&gt;
    text = cons.readLine();&lt;br /&gt;
    send(Chat.CMD_LOGIN + text);&lt;br /&gt;
    // Main thread blocks here&lt;br /&gt;
    while ((text = cons.readLine()) != null) {&lt;br /&gt;
      if (text.length() == 0 || text.charAt(0) == &amp;quot;#&amp;quot;)&lt;br /&gt;
        continue;      // ignore null lines and comments&lt;br /&gt;
      if (text.charAt(0) == &amp;quot;/&amp;quot;)&lt;br /&gt;
        send(text.substring(1));&lt;br /&gt;
      else send(&amp;quot;B&amp;quot;+text);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  protected void send(String s) {&lt;br /&gt;
    pw.println(s);&lt;br /&gt;
    pw.flush();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/** Constants and Class Methods for Java Chat Clients and Server.&lt;br /&gt;
 *&lt;br /&gt;
 * The protocol:&lt;br /&gt;
 *  --&amp;gt; Lusername&lt;br /&gt;
 *  --&amp;gt; Btext_to_broadcast&lt;br /&gt;
 *  --&amp;gt; Musername\Message&lt;br /&gt;
 *  --&amp;gt; Q&lt;br /&gt;
 *  &amp;lt;-- any text to be displayed.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Ian Darwin&lt;br /&gt;
 * @version $Id: Chat.java,v 1.3 2004/02/16 02:44:43 ian Exp $&lt;br /&gt;
 */&lt;br /&gt;
class Chat {&lt;br /&gt;
  // These are the first character of messages from client to server&lt;br /&gt;
  public static final int PORTNUM = 9999;&lt;br /&gt;
  public static final int MAX_LOGIN_LENGTH = 20;&lt;br /&gt;
  public static final char SEPARATOR = &amp;quot;\\&amp;quot;;&lt;br /&gt;
  public static final char COMMAND = &amp;quot;\\&amp;quot;;&lt;br /&gt;
  public static final char CMD_LOGIN = &amp;quot;L&amp;quot;;&lt;br /&gt;
  public static final char CMD_QUIT  = &amp;quot;Q&amp;quot;;&lt;br /&gt;
  public static final char CMD_MESG  = &amp;quot;M&amp;quot;;&lt;br /&gt;
  public static final char CMD_BCAST = &amp;quot;B&amp;quot;;&lt;br /&gt;
  // These are the first character of messages from server to client&lt;br /&gt;
  public static final char RESP_PUBLIC = &amp;quot;P&amp;quot;;&lt;br /&gt;
  public static final char RESP_PRIVATE = &amp;quot;M&amp;quot;;&lt;br /&gt;
  public static final char RESP_SYSTEM = &amp;quot;S&amp;quot;;&lt;br /&gt;
  // TODO in main loop:&lt;br /&gt;
  // if (text.charAt(0) == &amp;quot;/&amp;quot;)&lt;br /&gt;
  //    send(text);&lt;br /&gt;
  // else send(&amp;quot;B&amp;quot;+text);&lt;br /&gt;
  public static boolean isValidLoginName(String login) {&lt;br /&gt;
    // check length&lt;br /&gt;
    if (login.length() &amp;gt; MAX_LOGIN_LENGTH)&lt;br /&gt;
      return false;&lt;br /&gt;
    // check for bad chars&lt;br /&gt;
    // if (contains bad chars)&lt;br /&gt;
    //  return false&lt;br /&gt;
    // Passed above tests, is OK&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/** Trivial Chat Server to go with our Trivial Chat Client.&lt;br /&gt;
 *&lt;br /&gt;
 * WARNING -- this code is believed thread-safe but has NOT been 100% vetted &lt;br /&gt;
 * by a team of world-class experts for Thread-safeness.&lt;br /&gt;
 * DO NOT BUILD ANYTHING CRITICAL BASED ON THIS until you have done so.&lt;br /&gt;
 * See the various books on Threaded Java for design issues.&lt;br /&gt;
 *&lt;br /&gt;
 * @author  Ian F. Darwin, http://www.darwinsys.ru/&lt;br /&gt;
 * @version $Id: ChatServer.java,v 1.10 2004/03/13 21:56:32 ian Exp $&lt;br /&gt;
 */&lt;br /&gt;
class ChatServer {&lt;br /&gt;
  /** What I call myself in system messages */&lt;br /&gt;
  protected final static String CHATMASTER_ID = &amp;quot;ChatMaster&amp;quot;;&lt;br /&gt;
  /** What goes between any handle and the message */&lt;br /&gt;
  protected final static String SEP = &amp;quot;: &amp;quot;;&lt;br /&gt;
  /** The Server Socket */&lt;br /&gt;
  protected ServerSocket servSock;&lt;br /&gt;
  /** The list of my current clients */&lt;br /&gt;
  protected ArrayList clients;&lt;br /&gt;
  /** Debugging state */&lt;br /&gt;
  private static boolean DEBUG = false;&lt;br /&gt;
  /** Main just constructs a ChatServer, which should never return */&lt;br /&gt;
  public static void main(String[] argv) {&lt;br /&gt;
    System.out.println(&amp;quot;DarwinSys Chat Server 0.1 starting...&amp;quot;);&lt;br /&gt;
    if (argv.length == 1 &amp;amp;&amp;amp; argv[0].equals(&amp;quot;-debug&amp;quot;))&lt;br /&gt;
      DEBUG = true;&lt;br /&gt;
    ChatServer w = new ChatServer();&lt;br /&gt;
    w.runServer();      // should never return.&lt;br /&gt;
    System.out.println(&amp;quot;**ERROR* Chat Server 0.1 quitting&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  /** Construct (and run!) a Chat Service */&lt;br /&gt;
  ChatServer() {&lt;br /&gt;
    clients = new ArrayList();&lt;br /&gt;
    try {&lt;br /&gt;
      servSock = new ServerSocket(Chat.PORTNUM);&lt;br /&gt;
      System.out.println(&amp;quot;DarwinSys Chat Server Listening on port &amp;quot; +&lt;br /&gt;
        Chat.PORTNUM);&lt;br /&gt;
    } catch(IOException e) {&lt;br /&gt;
      log(&amp;quot;IO Exception in ChatServer.&amp;lt;init&amp;gt;&amp;quot; + e);&lt;br /&gt;
      System.exit(0);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void runServer() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        Socket us = servSock.accept();&lt;br /&gt;
        String hostName = us.getInetAddress().getHostName();&lt;br /&gt;
        System.out.println(&amp;quot;Accepted from &amp;quot; + hostName);&lt;br /&gt;
        ChatHandler cl = new ChatHandler(us, hostName);&lt;br /&gt;
        synchronized (clients) {&lt;br /&gt;
          clients.add(cl);&lt;br /&gt;
          cl.start();&lt;br /&gt;
          if (clients.size() == 1)&lt;br /&gt;
            cl.send(CHATMASTER_ID, &amp;quot;Welcome! you&amp;quot;re the first one here&amp;quot;);&lt;br /&gt;
          else {&lt;br /&gt;
            cl.send(CHATMASTER_ID, &amp;quot;Welcome! you&amp;quot;re the latest of &amp;quot; +&lt;br /&gt;
              clients.size() + &amp;quot; users.&amp;quot;);&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    } catch(IOException e) {&lt;br /&gt;
      log(&amp;quot;IO Exception in runServer: &amp;quot; + e);&lt;br /&gt;
      System.exit(0);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  protected void log(String s) {&lt;br /&gt;
    System.out.println(s);&lt;br /&gt;
  }&lt;br /&gt;
  /** Inner class to handle one conversation */&lt;br /&gt;
  protected class ChatHandler extends Thread {&lt;br /&gt;
    /** The client socket */&lt;br /&gt;
    protected Socket clientSock;&lt;br /&gt;
    /** BufferedReader for reading from socket */&lt;br /&gt;
    protected BufferedReader is;&lt;br /&gt;
    /** PrintWriter for sending lines on socket */&lt;br /&gt;
    protected PrintWriter pw;&lt;br /&gt;
    /** The client&amp;quot;s host */&lt;br /&gt;
    protected String clientIP;&lt;br /&gt;
    /** String form of user&amp;quot;s handle (name) */&lt;br /&gt;
    protected String login;&lt;br /&gt;
    /* Construct a Chat Handler */&lt;br /&gt;
    public ChatHandler(Socket sock, String clnt) throws IOException {&lt;br /&gt;
      clientSock = sock;&lt;br /&gt;
      clientIP = clnt;&lt;br /&gt;
      is = new BufferedReader(&lt;br /&gt;
        new InputStreamReader(sock.getInputStream()));&lt;br /&gt;
      pw = new PrintWriter(sock.getOutputStream(), true);&lt;br /&gt;
    }&lt;br /&gt;
    /** Each ChatHandler is a Thread, so here&amp;quot;s the run() method,&lt;br /&gt;
     * which handles this conversation.&lt;br /&gt;
     */&lt;br /&gt;
    public void run() {&lt;br /&gt;
      String line;&lt;br /&gt;
      try {&lt;br /&gt;
        while ((line = is.readLine()) != null) {&lt;br /&gt;
          char c = line.charAt(0);&lt;br /&gt;
          line = line.substring(1);&lt;br /&gt;
          switch (c) {&lt;br /&gt;
          case Chat.CMD_LOGIN:&lt;br /&gt;
            if (!Chat.isValidLoginName(line)) {&lt;br /&gt;
              send(CHATMASTER_ID, &amp;quot;LOGIN &amp;quot; + line + &amp;quot; invalid&amp;quot;);&lt;br /&gt;
              log(&amp;quot;LOGIN INVALID from &amp;quot; + clientIP);&lt;br /&gt;
              continue;&lt;br /&gt;
            }&lt;br /&gt;
            login = line;&lt;br /&gt;
            broadcast(CHATMASTER_ID, login + &lt;br /&gt;
              &amp;quot; joins us, for a total of &amp;quot; + &lt;br /&gt;
              clients.size() + &amp;quot; users&amp;quot;);&lt;br /&gt;
            break;&lt;br /&gt;
          case Chat.CMD_MESG:&lt;br /&gt;
            if (login == null) {&lt;br /&gt;
              send(CHATMASTER_ID, &amp;quot;please login first&amp;quot;);&lt;br /&gt;
              continue;&lt;br /&gt;
            }&lt;br /&gt;
            int where = line.indexOf(Chat.SEPARATOR);&lt;br /&gt;
            String recip = line.substring(0, where);&lt;br /&gt;
            String mesg = line.substring(where+1);&lt;br /&gt;
            log(&amp;quot;MESG: &amp;quot; + login + &amp;quot;--&amp;gt;&amp;quot; + recip + &amp;quot;: &amp;quot;+ mesg);&lt;br /&gt;
            ChatHandler cl = lookup(recip);&lt;br /&gt;
            if (cl == null)&lt;br /&gt;
              psend(CHATMASTER_ID, recip + &amp;quot; not logged in.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
              cl.psend(login, mesg);&lt;br /&gt;
            break;&lt;br /&gt;
          case Chat.CMD_QUIT:&lt;br /&gt;
            broadcast(CHATMASTER_ID,&lt;br /&gt;
              &amp;quot;Goodbye to &amp;quot; + login + &amp;quot;@&amp;quot; + clientIP);&lt;br /&gt;
            close();&lt;br /&gt;
            return;    // The end of this ChatHandler&lt;br /&gt;
            &lt;br /&gt;
          case Chat.CMD_BCAST:&lt;br /&gt;
            if (login != null)&lt;br /&gt;
              broadcast(login, line);&lt;br /&gt;
            else&lt;br /&gt;
              log(&amp;quot;B&amp;lt;L FROM &amp;quot; + clientIP);&lt;br /&gt;
            break;&lt;br /&gt;
          default:&lt;br /&gt;
            log(&amp;quot;Unknown cmd &amp;quot; + c + &amp;quot; from &amp;quot; + login + &amp;quot;@&amp;quot; + clientIP);&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
        log(&amp;quot;IO Exception: &amp;quot; + e);&lt;br /&gt;
      } finally {&lt;br /&gt;
        // the sock ended, so we&amp;quot;re done, bye now&lt;br /&gt;
        System.out.println(login + SEP + &amp;quot;All Done&amp;quot;);&lt;br /&gt;
        synchronized(clients) {&lt;br /&gt;
          clients.remove(this);&lt;br /&gt;
          if (clients.size() == 0) {&lt;br /&gt;
            System.out.println(CHATMASTER_ID + SEP +&lt;br /&gt;
              &amp;quot;I&amp;quot;m so lonely I could cry...&amp;quot;);&lt;br /&gt;
          } else if (clients.size() == 1) {&lt;br /&gt;
            ChatHandler last = (ChatHandler)clients.get(0);&lt;br /&gt;
            last.send(CHATMASTER_ID,&lt;br /&gt;
              &amp;quot;Hey, you&amp;quot;re talking to yourself again&amp;quot;);&lt;br /&gt;
          } else {&lt;br /&gt;
            broadcast(CHATMASTER_ID,&lt;br /&gt;
              &amp;quot;There are now &amp;quot; + clients.size() + &amp;quot; users&amp;quot;);&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    protected void close() {&lt;br /&gt;
      if (clientSock == null) {&lt;br /&gt;
        log(&amp;quot;close when not open&amp;quot;);&lt;br /&gt;
        return;&lt;br /&gt;
      }&lt;br /&gt;
      try {&lt;br /&gt;
        clientSock.close();&lt;br /&gt;
        clientSock = null;&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
        log(&amp;quot;Failure during close to &amp;quot; + clientIP);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    /** Send one message to this user */&lt;br /&gt;
    public void send(String sender, String mesg) {&lt;br /&gt;
      pw.println(sender + SEP + mesg);&lt;br /&gt;
    }&lt;br /&gt;
    /** Send a private message */&lt;br /&gt;
    protected void psend(String sender, String msg) {&lt;br /&gt;
      send(&amp;quot;&amp;lt;*&amp;quot; + sender + &amp;quot;*&amp;gt;&amp;quot;, msg);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /** Send one message to all users */&lt;br /&gt;
    public void broadcast(String sender, String mesg) {&lt;br /&gt;
      System.out.println(&amp;quot;Broadcasting &amp;quot; + sender + SEP + mesg);&lt;br /&gt;
      for (int i=0; i&amp;lt;clients.size(); i++) {&lt;br /&gt;
        ChatHandler sib = (ChatHandler)clients.get(i);&lt;br /&gt;
        if (DEBUG)&lt;br /&gt;
          System.out.println(&amp;quot;Sending to &amp;quot; + sib);&lt;br /&gt;
        sib.send(sender, mesg);&lt;br /&gt;
      }&lt;br /&gt;
      if (DEBUG) System.out.println(&amp;quot;Done broadcast&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    protected ChatHandler lookup(String nick) {&lt;br /&gt;
      synchronized(clients) {&lt;br /&gt;
        for (int i=0; i&amp;lt;clients.size(); i++) {&lt;br /&gt;
          ChatHandler cl = (ChatHandler)clients.get(i);&lt;br /&gt;
          if (cl.login.equals(nick))&lt;br /&gt;
            return cl;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    /** Present this ChatHandler as a String */&lt;br /&gt;
    public String toString() {&lt;br /&gt;
      return &amp;quot;ChatHandler[&amp;quot; + login + &amp;quot;]&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/** &lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * Simple Chat Room Applet.&lt;br /&gt;
 * Writing a Chat Room seems to be one of many obligatory rites (or wrongs)&lt;br /&gt;
 * of passage for Java experts these days.&amp;lt;/p&amp;gt;&lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * This one is a toy because it doesn&amp;quot;t have much of a protocol, which&lt;br /&gt;
 * means we can&amp;quot;t query the server as to * who&amp;quot;s logged in,&lt;br /&gt;
 * or anything fancy like that. However, it works OK for small groups.&amp;lt;/p&amp;gt;&lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * Uses client socket w/ two Threads (main and one constructed),&lt;br /&gt;
 * one for reading and one for writing.&amp;lt;/p&amp;gt;&lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * Server multiplexes messages back to all clients.&amp;lt;/p&amp;gt;&lt;br /&gt;
 * @author Ian Darwin&lt;br /&gt;
 * @version $Id: ChatRoom.java,v 1.8 2004/03/09 03:59:37 ian Exp $&lt;br /&gt;
 */&lt;br /&gt;
class ChatRoom extends Applet {&lt;br /&gt;
  /** Whether we are being run as an Applet or an Application */&lt;br /&gt;
  protected boolean inAnApplet = true;&lt;br /&gt;
  /** The state of logged-in-ness */&lt;br /&gt;
  protected boolean loggedIn;&lt;br /&gt;
  /* The Frame, for a pop-up, durable Chat Room. */&lt;br /&gt;
  protected Frame cp;&lt;br /&gt;
  /** The default port number */&lt;br /&gt;
  protected static int PORTNUM = Chat.PORTNUM;&lt;br /&gt;
  /** The actual port number */&lt;br /&gt;
  protected int port;&lt;br /&gt;
  /** The network socket */&lt;br /&gt;
  protected Socket sock;&lt;br /&gt;
  /** BufferedReader for reading from socket */&lt;br /&gt;
  protected BufferedReader is;&lt;br /&gt;
  /** PrintWriter for sending lines on socket */&lt;br /&gt;
  protected PrintWriter pw;&lt;br /&gt;
  /** TextField for input */&lt;br /&gt;
  protected TextField tf;&lt;br /&gt;
  /** TextArea to display conversations */&lt;br /&gt;
  protected TextArea ta;&lt;br /&gt;
  /** The Login button */&lt;br /&gt;
  protected Button lib;&lt;br /&gt;
  /** The LogOUT button */&lt;br /&gt;
  protected Button lob;&lt;br /&gt;
  /** The TitleBar title */&lt;br /&gt;
  final static String TITLE = &amp;quot;Chat: Ian Darwin&amp;quot;s Toy Chat Room Client&amp;quot;;&lt;br /&gt;
  /** The message that we paint */&lt;br /&gt;
  protected String paintMessage;&lt;br /&gt;
  /** init, overriding the version inherited from Applet */&lt;br /&gt;
  public void init() {&lt;br /&gt;
    paintMessage = &amp;quot;Creating Window for Chat&amp;quot;;&lt;br /&gt;
    repaint();&lt;br /&gt;
    cp = new Frame(TITLE);&lt;br /&gt;
    cp.setLayout(new BorderLayout());&lt;br /&gt;
    String portNum = null;&lt;br /&gt;
    if (inAnApplet)&lt;br /&gt;
      portNum = getParameter(&amp;quot;port&amp;quot;);&lt;br /&gt;
    port = PORTNUM;&lt;br /&gt;
    if (portNum != null)&lt;br /&gt;
      port = Integer.parseInt(portNum);&lt;br /&gt;
    // The GUI&lt;br /&gt;
    ta = new TextArea(14, 80);&lt;br /&gt;
    ta.setEditable(false);    // readonly&lt;br /&gt;
    ta.setFont(new Font(&amp;quot;Monospaced&amp;quot;, Font.PLAIN, 11));&lt;br /&gt;
    cp.add(BorderLayout.NORTH, ta);&lt;br /&gt;
    Panel p = new Panel();&lt;br /&gt;
    Button b;&lt;br /&gt;
    // The login button&lt;br /&gt;
    p.add(lib = new Button(&amp;quot;Login&amp;quot;));&lt;br /&gt;
    lib.setEnabled(true);&lt;br /&gt;
    lib.requestFocus();&lt;br /&gt;
    lib.addActionListener(new ActionListener() {&lt;br /&gt;
      public void actionPerformed(ActionEvent e) {&lt;br /&gt;
        login();&lt;br /&gt;
        lib.setEnabled(false);&lt;br /&gt;
        lob.setEnabled(true);&lt;br /&gt;
        tf.requestFocus();  // set keyboard focus in right place!&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // The logout button&lt;br /&gt;
    p.add(lob = new Button(&amp;quot;Logout&amp;quot;));&lt;br /&gt;
    lob.setEnabled(false);&lt;br /&gt;
    lob.addActionListener(new ActionListener() {&lt;br /&gt;
      public void actionPerformed(ActionEvent e) {&lt;br /&gt;
        logout();&lt;br /&gt;
        lib.setEnabled(true);&lt;br /&gt;
        lob.setEnabled(false);&lt;br /&gt;
        lib.requestFocus();&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    p.add(new Label(&amp;quot;Message here:&amp;quot;));&lt;br /&gt;
    tf = new TextField(40);&lt;br /&gt;
    tf.addActionListener(new ActionListener() {&lt;br /&gt;
      public void actionPerformed(ActionEvent e) {&lt;br /&gt;
        if (loggedIn) {&lt;br /&gt;
          pw.println(Chat.CMD_BCAST+tf.getText());&lt;br /&gt;
          tf.setText(&amp;quot;&amp;quot;); &lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    p.add(tf);&lt;br /&gt;
    cp.add(BorderLayout.SOUTH, p);&lt;br /&gt;
        cp.addWindowListener(new WindowAdapter() {&lt;br /&gt;
      public void windowClosing(WindowEvent e) {&lt;br /&gt;
        // If we do setVisible and dispose, then the Close completes&lt;br /&gt;
        ChatRoom.this.cp.setVisible(false);&lt;br /&gt;
        ChatRoom.this.cp.dispose();&lt;br /&gt;
        logout();&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    cp.pack();&lt;br /&gt;
    // After packing the Frame, centre it on the screen.&lt;br /&gt;
    Dimension us = cp.getSize(), &lt;br /&gt;
      them = Toolkit.getDefaultToolkit().getScreenSize();&lt;br /&gt;
    int newX = (them.width - us.width) / 2;&lt;br /&gt;
    int newY = (them.height- us.height)/ 2;&lt;br /&gt;
    cp.setLocation(newX, newY);&lt;br /&gt;
    cp.setVisible(true);&lt;br /&gt;
    paintMessage = &amp;quot;Window should now be visible&amp;quot;;&lt;br /&gt;
    repaint();&lt;br /&gt;
  }&lt;br /&gt;
  protected String serverHost = &amp;quot;localhost&amp;quot;;&lt;br /&gt;
  /** LOG ME IN TO THE CHAT */&lt;br /&gt;
  public void login() {&lt;br /&gt;
    showStatus(&amp;quot;In login!&amp;quot;);&lt;br /&gt;
    if (loggedIn)&lt;br /&gt;
      return;&lt;br /&gt;
    if (inAnApplet)&lt;br /&gt;
      serverHost = getCodeBase().getHost();&lt;br /&gt;
    try {&lt;br /&gt;
      sock = new Socket(serverHost, port);&lt;br /&gt;
      is = new BufferedReader(new InputStreamReader(sock.getInputStream()));&lt;br /&gt;
      pw = new PrintWriter(sock.getOutputStream(), true);&lt;br /&gt;
    } catch(IOException e) {&lt;br /&gt;
      showStatus(&amp;quot;Can&amp;quot;t get socket to &amp;quot; + &lt;br /&gt;
        serverHost + &amp;quot;/&amp;quot; + port + &amp;quot;: &amp;quot; + e);&lt;br /&gt;
      cp.add(new Label(&amp;quot;Can&amp;quot;t get socket: &amp;quot; + e));&lt;br /&gt;
      return;&lt;br /&gt;
    }&lt;br /&gt;
    showStatus(&amp;quot;Got socket&amp;quot;);&lt;br /&gt;
    // Construct and start the reader: from server to textarea.&lt;br /&gt;
    // Make a Thread to avoid lockups.&lt;br /&gt;
    new Thread(new Runnable() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        String line;&lt;br /&gt;
        try {&lt;br /&gt;
          while (loggedIn &amp;amp;&amp;amp; ((line = is.readLine()) != null))&lt;br /&gt;
            ta.append(line + &amp;quot;\n&amp;quot;);&lt;br /&gt;
        } catch(IOException e) {&lt;br /&gt;
          showStatus(&amp;quot;GAA! LOST THE LINK!!&amp;quot;);&lt;br /&gt;
          return;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }).start();&lt;br /&gt;
    // FAKE LOGIN FOR NOW&lt;br /&gt;
    pw.println(Chat.CMD_LOGIN + &amp;quot;AppletUser&amp;quot;);&lt;br /&gt;
    loggedIn = true;&lt;br /&gt;
  }&lt;br /&gt;
  /** Log me out, Scotty, there&amp;quot;s no intelligent life here! */&lt;br /&gt;
  public void logout() {&lt;br /&gt;
    if (!loggedIn)&lt;br /&gt;
      return;&lt;br /&gt;
    loggedIn = false;&lt;br /&gt;
    try {&lt;br /&gt;
      if (sock != null)&lt;br /&gt;
        sock.close();&lt;br /&gt;
    } catch (IOException ign) {&lt;br /&gt;
      // so what?&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // It is deliberate that there is no STOP method - we want to keep&lt;br /&gt;
  // going even if the user moves the browser to another page.&lt;br /&gt;
  // Anti-social? Maybe, but you can use the CLOSE button to kill &lt;br /&gt;
  // the Frame, or you can exit the Browser.&lt;br /&gt;
  /** Paint paints the small window that appears in the HTML,&lt;br /&gt;
   * telling the user to look elsewhere!&lt;br /&gt;
   */&lt;br /&gt;
  public void paint(Graphics g) {&lt;br /&gt;
    Dimension d = getSize();&lt;br /&gt;
    int h = d.height;&lt;br /&gt;
    int w = d.width;&lt;br /&gt;
    g.fillRect(0, 0, w, 0);&lt;br /&gt;
    g.setColor(Color.black);&lt;br /&gt;
    g.drawString(paintMessage, 10, (h/2)-5);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** a showStatus that works for Applets or non-Applets alike */&lt;br /&gt;
  public void showStatus(String mesg) {&lt;br /&gt;
    if (inAnApplet)&lt;br /&gt;
      super.showStatus(mesg);&lt;br /&gt;
    System.out.println(mesg);&lt;br /&gt;
  }&lt;br /&gt;
  /** A main method to allow the client to be run as an Application */&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ChatRoom room101 = new ChatRoom();&lt;br /&gt;
    room101.inAnApplet = false;&lt;br /&gt;
    room101.init();&lt;br /&gt;
    room101.start();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>