Example 2
Home ] Up ] The Socket Class ] Example 1 ] [ Example 2 ]

 

 

Here's another example -- an applet that attempts to display the users logged in at the applet's web site, using the finger protocol:

Did you forget to enable Java in your browser?

If the host from which the applet was loaded does not have a finger server, then you may get a message:

java.net.ConnectException: Connection refused

(This is certainly the case for the current web site!)

Here's the source for the above applet:

package networking;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;

import java.net.Socket;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *   Applet to attempt to discover who is
 *   logged in on the applet's server.
 */
public class Who extends JApplet implements Runnable
{
  public void init()
  {
    if (!m_laidOut)
    {
      // Set attributes of the "Who?" button
      m_who.setFont( new Font("SansSerif", Font.PLAIN, 14) );
      m_who.addActionListener( new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            new Thread(Who.this).start();
          }
        }
      );
      
      // Add it to the applet
      JPanel contentPane = new JPanel();
      contentPane.add(m_who);
      setContentPane(contentPane);
      
      m_laidOut = true;
    }
  }
  
  /**
   *   Runs when the thread is started.
   */
  public void run()
  {
    try
    {
      // Prevent multiple simultaneous activations.
      m_who.setEnabled(false);
      
      // Create a Who frame to display the results.
      WhoFrame frame =
          new WhoFrame("Who's logged on: Connecting...");
      frame.setVisible(true);
      
      // Show the logins found
      showLogons(frame);
    }
    finally
    {
      // Reenable button after we've grabbed the information,
      // regardless of success.
      m_who.setEnabled(true);
    }
  }
  
  /**
   *   Shows the logins returned by finger.
   */
  private void showLogons(WhoFrame frame)
  {
    JTextArea area = frame.getTextArea();
    
    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    try
    {
      // Get the applet's host name
      String hostName = getCodeBase().getHost();
      
      // Connect to a finger server, if any
      socket = new Socket(hostName, 79);
      
      // Grab the output and input streams
      // (true means autoflush)
      out = new PrintWriter( socket.getOutputStream(), true );
      in  = new BufferedReader(
          new InputStreamReader( socket.getInputStream() ) );
      // Send a blank line to the finger server
      // telling it to supply a listing of everyone logged on
      out.println();
      
      // Read the server's response and display
      // it in the text area
      String line = null;
      while (true)
      {
        line = in.readLine();
        if (line == null)
          break;
        area.append(line + "\n");
      }
      
      frame.setTitle("Who's Logged On: " + hostName);
    }
    catch (IOException e)
    {
      area.append(e.toString());
      frame.setTitle("Who's Logged On: Error!");
    }
    finally
    {
      try
      {
        if (in != null)
          in.close();
        if (out != null)
          out.close();
        if (socket != null)
          socket.close();
      }
      catch (Exception e)
      {
        // Do nothing
      }
    }
  }
  
  // Private data //
  private boolean   m_laidOut = false;
  private JButton  m_who = new JButton("Who?");
}

/**
 *   A Frame that closes properly on a windows closing event,
 *   and that contains a text area to write into.
 */
class WhoFrame extends JFrame
{
  public WhoFrame(String title)
  {
    super(title);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
    m_area.setFont( new Font("MonoSpaced", Font.PLAIN, 14) );
    getContentPane().add(m_area, BorderLayout.CENTER);
    pack();
  }
  
  public JTextArea getTextArea()
  {
    return m_area;
  }
  
  // Private data //
  private JTextArea m_area = new JTextArea(10, 80);
}
 
The page was last updated February 19, 2008