/******************************************************************* Title: Java FingerApplet Client Authors: Dr. Roger Webster Date: April 19, 1998 Filename: FingerApplet.java WWW server: http://cs.millersv.edu/ *******************************************************************/ import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; /** * This applet connects to the "finger" server on the host * it was served from to determine who is currently logged on. * Because it is an untrusted applet, it can only connect to the host * from which it came. Since web servers do not usually run finger * servers themselves, this applet will often be used in conjunction * with a proxy server, to serve it from some other host that does run * a finger server. **/ public class FingerApplet extends Applet implements Runnable { Button who; DataInputStream in; PrintStream out; String returnline; Socket socket; Thread mythread; TextField name = new TexField(40); List mylist = new List (60, false); public void init() { who = new Button("Show who is Logged on"); who.setFont(new Font("SansSerif", Font.PLAIN, 14)); this.add(who); this.add(name); this.show(); } /** * This is the method that does the networking and displays the results. * It is implemented as the body of a separate thread because it might * take some time to complete, and applet methods need to return promptly. **/ public void run() { try { // Find out who's logged on // Connect to port 79 (the standard finger port) on the host // that the applet was loaded from. Connect(); ReadSocket(); PopUpWindow window = new PopUpWindow("Who is on", returnline); } catch(Exception e) //RWW added to catch ALL { e.printStackTrace(); PopUpWindow window = new PopUpWindow(" Problems", e.toString()); System.out.println("Socket Exception:" + e.toString()); } } // uses the jdk1.0.2 event handler... public boolean handleEvent(Event event) { int x, y; switch (event.id) { case Event.ACTION_EVENT: { System.out.println("mouse down"); x = event.x; y = event.y; new Thread(this).start(); } } return true; } public void ReadSocket() { // Send a blank line to the finger server, telling it that we want // a listing of everyone logged on instead of information about an // individual user. StringBuffer mystring = new StringBuffer(578); String line; try { out.println(); out.flush(); // Send it now! for(;;) { line = in.readLine(); System.out.println("line:" + line + "\n"); if (line == null) break; mystring.append(line); System.out.println("mystring.append:" + mystring.toString() + "\n"); } returnline = mystring.toString(); } catch(Exception e) { e.printStackTrace(); System.out.println("Error:" + e.toString()); PopUpWindow window = new PopUpWindow("Problems read socket", e.toString()); } } public void Connect() { try { socket = new Socket(this.getCodeBase().getHost(), 79); out = new PrintStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); } catch(Exception e) //RWW added to catch ALL { e.printStackTrace(); PopUpWindow window = new PopUpWindow("Socket Problems", e.toString()); System.out.println("Socket Exception:" + e.toString()); } } } // end init class class PopUpWindow extends Frame { public Panel buttonPanel; public Button close; public PopUpWindow(String title, String mesg){ //Instantiate variables super(title); Label return_message = new Label(mesg); buttonPanel = new Panel(); close = new Button("OK"); //Add button to panel and panel to window setLayout(new FlowLayout()); buttonPanel.setLayout(new GridLayout(2,1)); buttonPanel.add(return_message); buttonPanel.add(close); add(buttonPanel); //Size window to preferred size this.reshape(100, 100, 500, 550); move(150,300); close.requestFocus(); show(); }//end PopUpWindow(String, String) // uses the jdk1.0.2 event handler... public boolean action(Event e, Object arg){ if(e.target instanceof Button){ if(e.target == close){ if(this.isShowing()) { dispose(); } } } return true; }//end action(Event, Object) }//end class definition