import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; import netscape.javascript.JSObject; public class ChatClient2 extends Applet { public static final int PORT = 2000; Socket s; DataInputStream in; PrintStream out; StreamListener2 listener; String output_name = "form1.output"; String user_name = "some_user"; public void init () { String value; if ((value = getParameter ("output")) != null) output_name = value; if ((value = getParameter ("user")) != null) user_name = value; try { s = new Socket (this.getCodeBase().getHost(), PORT); in = new DataInputStream (s.getInputStream ()); out = new PrintStream (s.getOutputStream ()); listener = new StreamListener2 (in, output_name, this); } catch (IOException e) { this.showStatus (e.toString ()); } } public void sendMessage (String msg) { out.println (user_name + ": " + msg); } public void setUser (String user) { user_name = user; } } class StreamListener2 extends Thread { DataInputStream in; String output_name; Applet appl; public StreamListener2 (DataInputStream in, String output_name, Applet appl) { this.in = in; this.output_name = output_name; this.appl = appl; this.start (); } public void run () { JSObject win = JSObject.getWindow (appl); String line; try { for (;;) { line = in.readLine (); if (line == null) break; win.eval ("document." + output_name + ".value += \"" + line + "\\n\""); } } catch (IOException e) { win.eval ("document." + output_name + ".value += \"" + e.toString () + "\\n\""); } finally { win.eval ("document." + output_name + ".value += \"Connection closed by server.\""); } } }