home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 3.3 KB | 169 lines |
-
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
-
- /*
- * application class
- */
- public class Ping extends Frame implements Runnable, ActionListener {
-
- /*
- * instance of Socket used for determining
- * whether remote host is alive
- */
- Socket socket;
-
- /*
- * hostname text entry field
- */
- TextField addrField;
-
- /*
- * the stop button
- */
- Button stopButton;
- Button startButton;
-
- /*
- * the text area for printing remote
- * host status
- */
- TextArea content;
-
- /*
- * thread used to decouple user activity
- * from socket activity
- */
- Thread thread;
-
- /*
- * text area insert position
- */
- int insertPos;
-
- /*
- * constructor creates user interface
- */
- public Ping () {
-
- int i;
-
- setTitle ("Ping");
- setLayout(new BorderLayout());
-
- addrField = new TextField ("", 20);
- startButton = new Button ("Start");
- stopButton = new Button ("Stop");
- content = new TextArea ("", 24, 80);
- content.setEditable (false);
- insertPos = 0;
-
- Panel p = new Panel ();
- p.setLayout (new FlowLayout ());
- p.add (addrField);
- p.add (startButton);
- p.add (stopButton);
-
- startButton.addActionListener(this);
- stopButton.addActionListener(this);
- addWindowListener(new WindowCloser());
-
-
- add ("North", p);
- add ("Center", content);
-
- thread = new Thread (this);
-
- setSize (400, 300);
- show ();
- }
-
- /**
- * handle action events
- * Enter keypress starts socket open thread
- * Stop button click stops socket thread
- * @param evt - event object
- * @param obj - object receiving this event
- */
- public void actionPerformed (ActionEvent ev)
- {
- Object object1 = ev.getSource();
- if (object1 == startButton)
- {
- if (thread != null)
- thread = null;
- thread = new Thread (this);
- thread.start ();
- }
- if (object1 == stopButton)
- {
- if (thread!= null)
- thread = null;
- }
- }
-
- /*
- * socket open thread
- * tries to open the telnet port (23)
- */
- public void run () {
-
- String name;
-
- name = addrField.getText ();
- try {
- try {
- socket = new Socket (name, 23);
- } catch (UnknownHostException e) {
- insertString (name+" :unknown host\n");
- return;
- }
- } catch (IOException e2) {
- insertString ("Unable to open socket\n");
- return;
- }
-
- insertString (name + " is alive\n");
- try {
- socket.close ();
- } catch (IOException e2) {
- insertString ("IOException closing socket\n");
-
- return;
- }
- }
-
- /**
- * helper method for adding text to the end
- * of the text area
- * @param s - text to add
- */
- void insertString (String s) {
-
- content.insert (s, insertPos);
- insertPos += s.length ();
- }
-
- /**
- * application entry point
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- new Ping ();
- }
- }
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-