// This example from the book Java in a Nutshell by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Assc. // You may study, modify, this example for any purpose. // This example is WITHOUT WARRANTY either expressed or implied. // Modiied by Kandice A. Null 4/96 to add a window and tearoff menu and buttons. import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class wind2 extends Applet { public static final int PORT = 6789; Socket s; DataInputStream in; PrintStream out; TextField inputfield; TextArea outputarea; StreamListener listener; Frame window; MenuBar menbar; Menu men; // Create a socket to communicate with a server on port 6789 of the // host that the applet's code is on. Create streams to use with // the socket. Then create a TextField for user input and a TextArea // for server output. Finally, create a thread to wait for and // display server output. public void init() { try { s = new Socket(this.getCodeBase().getHost(), PORT); in = new DataInputStream(s.getInputStream()); out = new PrintStream(s.getOutputStream()); System.out.println("here in applet!!"); this.add(new Button ("Open Window")); this.add(new Button ("Close Window")); this.add(new Button ("Quit Window")); window = new Frame("Client Window"); this.add(window); inputfield = new TextField(); outputarea = new TextArea(); window.setLayout (new BorderLayout()); window.add("North", inputfield); window.add("Center", outputarea); menbar = new MenuBar(); men = new Menu("Options",true); men.add(new MenuItem ("Open")); men.add(new MenuItem ("Close")); men.add(new MenuItem ("Quit")); menbar.add(men); menbar.setHelpMenu(men); window.setMenuBar(menbar); window.resize(500,300); // window.show(); listener = new StreamListener(in, outputarea); this.showStatus("Connected to " + s.getInetAddress().getHostName() + ":" + s.getPort()); } catch (IOException e) this.showStatus(e.toString()); } // When the user types a line, send it to the server. public boolean action(Event e, Object what) { if (e.target instanceof Button) { String label = (String)what; if (label.equals("Open Window")) { if (!window.isShowing()) window.show(); } else if (label.equals("Close Window")) { if (window.isShowing()) window.hide(); } else if (label.equals("Quit Window")) { window.dispose(); System.exit(0); } return true; } else if (e.target instanceof MenuItem) { String l = (String)what; if (l.equals("Open")) { if (!window.isShowing()) window.show(); } else if (l.equals("Close")) { if (window.isShowing()) window.hide(); } else if (l.equals("Quit")) { outputarea.setText("Quitting the Window"); inputfield.setText("Quit Window"); window.dispose(); System.exit(0); } return true; } else if (e.target == inputfield) { out.println((String)e.arg); inputfield.setText(""); return true; } else return false; } } // Wait for output from the server on the specified stream, and display // it in the specified TextArea. class StreamListener extends Thread { DataInputStream in; TextArea output; public StreamListener(DataInputStream in, TextArea output) { this.in = in; this.output = output; this.start(); } public void run() { String line; try { for(;;) { line = in.readLine(); if (line == null) break; output.setText(line); } } catch (IOException e) output.setText(e.toString()); finally output.setText("Connection closed by server."); } }