home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch10 / Ping.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  3.3 KB  |  169 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. /*
  8.  * application class
  9.  */
  10. public class Ping extends Frame implements Runnable, ActionListener {
  11.  
  12. /*
  13.  * instance of Socket used for determining
  14.  * whether remote host is alive
  15. */
  16. Socket socket;
  17.  
  18. /*
  19.  * hostname text entry field
  20.  */
  21. TextField addrField;
  22.  
  23. /*
  24.  * the stop button
  25.  */
  26. Button stopButton;
  27. Button startButton;
  28.  
  29. /*
  30.  * the text area for printing remote
  31.  * host status
  32.  */
  33. TextArea content;
  34.  
  35. /*
  36.  * thread used to decouple user activity
  37.  * from socket activity
  38.  */
  39. Thread thread;
  40.  
  41. /*
  42.  * text area insert position
  43.  */
  44. int insertPos;
  45.  
  46. /*
  47.  * constructor creates user interface
  48.  */
  49. public Ping () {
  50.  
  51.     int i; 
  52.  
  53.     setTitle ("Ping");
  54.     setLayout(new BorderLayout());
  55.  
  56.     addrField = new TextField ("", 20);
  57.     startButton = new Button ("Start");
  58.     stopButton = new Button ("Stop");
  59.     content = new TextArea ("", 24, 80);
  60.     content.setEditable (false);
  61.     insertPos = 0;
  62.  
  63.     Panel p = new Panel ();
  64.     p.setLayout (new FlowLayout ());
  65.     p.add (addrField);
  66.     p.add (startButton);
  67.     p.add (stopButton);
  68.  
  69.     startButton.addActionListener(this);
  70.     stopButton.addActionListener(this);
  71.     addWindowListener(new WindowCloser());
  72.  
  73.  
  74.     add ("North", p);
  75.     add ("Center", content);
  76.  
  77.     thread = new Thread (this);
  78.  
  79.     setSize (400, 300);
  80.     show ();
  81. }
  82.  
  83. /**
  84.  * handle action events
  85.  * Enter keypress starts socket open thread
  86.  * Stop button click stops socket thread
  87. * @param evt - event object
  88.  * @param obj - object receiving this event
  89.  */
  90. public void actionPerformed (ActionEvent ev)
  91. {
  92.     Object object1 = ev.getSource();
  93.     if (object1 == startButton)
  94.     {
  95.         if (thread != null)
  96.             thread = null;
  97.         thread = new Thread (this);
  98.         thread.start ();
  99.     }
  100.     if (object1 == stopButton)
  101.     {
  102.         if (thread!= null)
  103.            thread = null;
  104.     }
  105. }
  106.  
  107. /*
  108.  * socket open thread
  109.  * tries to open the telnet port (23)
  110.  */
  111. public void run () {
  112.  
  113.     String name;
  114.  
  115.     name = addrField.getText ();
  116.         try {
  117.                 try {
  118.                         socket = new Socket (name, 23); 
  119.                 } catch (UnknownHostException e) {
  120.                         insertString (name+" :unknown host\n");
  121.                         return;
  122.                 }
  123.         } catch (IOException e2) {
  124.                 insertString ("Unable to open socket\n");
  125.                 return;
  126.         }
  127.  
  128.     insertString (name + " is alive\n");
  129.     try {
  130.         socket.close ();
  131.         } catch (IOException e2) {
  132.                 insertString ("IOException closing socket\n");
  133.  
  134.                 return;
  135.         }
  136. }
  137.  
  138. /**
  139.  * helper method for adding text to the end
  140.  * of the text area
  141.  * @param s - text to add
  142.  */
  143. void insertString (String s) {
  144.  
  145.     content.insert (s, insertPos);
  146.     insertPos += s.length ();
  147. }
  148.  
  149. /**
  150.  * application entry point
  151.  * @param args - command-line arguments
  152.  */
  153. public static void main (String args[]) {
  154.  
  155.     new Ping ();
  156. }
  157. }
  158.  
  159. class WindowCloser extends WindowAdapter
  160. {
  161.     public void windowClosing(WindowEvent e)
  162.     {
  163.         Window win = e.getWindow();
  164.         win.setVisible(false);
  165.         win.dispose();
  166.         System.exit(0);
  167.     }
  168. }
  169.