/******************************************************************* Title: Java FingerApplet Client Authors: Dr. Roger Webster Date: April 19, 1998 Fileperson_name: 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 { Button who; DataInputStream in; PrintStream out; String returnline; Socket socket; Thread mythread; TextField person_name; List mylist = new List (60, false); Label msg = new Label ("Type person's first or last name below to see their email address"); Label msg2 = new Label ("(after typing hit enter)"); boolean send_person_name = false; public void init() { who = new Button("Show everyone who is Logged on"); who.setFont(new Font("SansSerif", Font.PLAIN, 14)); this.add(msg); this.add(msg2); person_name = new TextField(40); person_name.setText(""); person_name.setEditable(true); this.add(person_name); this.add(who); 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 myrun() { 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(); // Send a blank line (or person_name) to the finger server, // telling it that we want // a listing of everyone logged on or information about an // individual user. if (send_person_name) { System.out.println("send_person_name is true:"); out.println(person_name.getText()); out.flush(); // Send it now! person_name.setText(""); } else { System.out.println("send_person_name is FALSE:"); out.println(); out.flush(); // Send it now! } ReadSocket(); TextWindow textwindow = new TextWindow("Results of Finger Applet", mylist); //erase textarea mylist.addItem(line); } catch(Exception e) //RWW added to catch ALL { e.printStackTrace(); PopUpWindow window = new PopUpWindow(" Problems", e.toString()); System.out.println("Socket Exception:" + e.toString()); } } // When the user types a line, send it to the server. public boolean action(Event e, Object what) { if (e.target == person_name) { System.out.println("person_name event"); send_person_name = true; myrun(); return true; } else if (e.target == who) { send_person_name = false; System.out.println("who button event"); myrun(); return true; } return false; } public void ReadSocket() { StringBuffer mystring = new StringBuffer(578); String line; try { for(;;) { line = in.readLine(); if (line == null) break; System.out.println("line:" + line + "\n"); mylist.addItem(line); mylist.addItem("\n"); } } 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 class TextWindow extends Frame { public Panel buttonPanel; public Panel textPanel; public Button close; public TextWindow (String title, List mystrings) { super(title); TextArea mytext = new TextArea(20,60); int x; int count; textPanel = new Panel(); close = new Button("OK Close this Window"); buttonPanel = new Panel(); //Add button to panel and panel to window setLayout(new FlowLayout(FlowLayout.CENTER, 15,15)); buttonPanel.setLayout(new GridLayout(10,1)); count = mystrings.countItems(); System.out.println("count "); System.out.println(count); for (x=0; x < count; x++) { mytext.appendText(mystrings.getItem(x) + "\n"); System.out.println("mytext:" + mystrings.getItem(x)); } textPanel.add(mytext); buttonPanel.add(close); this.add(textPanel); this.add(buttonPanel); //Size window to preferred size x,y,width,height this.reshape(10, 10, 100, 80); this.pack(); //move(150,300); close.requestFocus(); this.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