// example from the book Java in a Nutshell by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly // MODIFIED by Adam Plowcha & Dr. Roger Webster import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class cs406window extends Applet { public static final int PORT = 6789; Socket s; DataInputStream in; PrintStream out; TextField inputfield; TextArea outputarea; StreamListener listener; Frame window; // Create a socket to communicate with server on port 6789 of // host that the applet's is on. Create streams to use with // the socket. 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()); //creates input output fields... inputfield = new TextField(); outputarea = new TextArea(); // No typing in output field... outputarea.setEditable(false); listener = new StreamListener(in, outputarea); //Buttons to panel on page... this.setLayout(new FlowLayout()); this.add(new Button("OPEN")); this.add(new Button("CLOSE")); // Open window outside of page... window = new Frame ("CS406 Client-Server Communication"); this.add(window); // Add stuff like menu, tear-off flag, etc... window.setLayout (new BorderLayout()); window.add("North", inputfield); window.add("Center", outputarea); MenuBar a = new MenuBar(); Menu b = new Menu("Options", true); b.add(new MenuItem("Open")); b.add(new MenuItem("Close")); b.add(new MenuItem("Quit")); a.add(b); window.setMenuBar(a); // Display all the stuff just created... window.pack(); window.show(); show(); this.showStatus("Connected to " + s.getInetAddress().getHostName() + ":" + s.getPort()); } catch (IOException e) this.showStatus(e.toString()); } // Button and Menu events... public boolean action(Event e, Object what) { if (e.target instanceof MenuItem) { String label = (String)what; if (label.equals("Open")){ if (!window.isShowing()) window.show(); } else if (label.equals("Close")){ if (window.isShowing()) window.hide(); } else if (label.equals ("Quit")) { outputarea.setText("quit the window"); inputfield.setText("quit the window"); //window.hide(); //window.dispose(); } return true; } else if (e.target instanceof Button) { String label = (String)what; if (label.equals("OPEN")){ if (!window.isShowing()) window.show(); } else if (label.equals ("CLOSE")){ if (window.isShowing()) window.hide(); } 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."); } }