Applet Client

The other examples discussed were built as Java applications. Here, we show a revised version of the mid-size client program, recast as an applet. There are surprisingly few differences in the code, the main distinctions being subclassing Applet instead of Frame and the use of an init method instead of main.

The most important distinction lies with how the server and client must be configured with respect to hosts. Security restrictions limit the IP address to which the client can connect to the IP address of the WWW server from where the applet was originally obtained. Thus, your server must be running on the same host machine as the WWW server that provides access to your client applet.

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Client_Applet extends Applet implements ActionListener, WindowListener{

//**************** single-threaded applet client, with UI
//****************   default looks for server at jbspc.cs.unc.edu:8901

Socket connection;

InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;

public static final String DEFAULT_HOST = "jbspc.cs.unc.edu";
public static final int DEFAULT_PORT = 8901;
String host;
int port;

String message;

MyFrame outerBox;
TextField hostDisplay, portDisplay;
TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button sendButton, quitButton;



  public Client_Applet ()  {
    super (  );
  }  // end Client_Applet constructor

  public void init (  )  {

    buildUI ();

  }  // end init



// **************  Primary Listen & Respond Method

  public void connectClient ( )  {

    host = hostDisplay.getText ();
    if ( host.equals ("" ) ) host = DEFAULT_HOST;
    if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );
    else port = DEFAULT_PORT;
    logDisplay.appendText ( "Contacting server on "+host+", port "+port+"\n" );

    try  {
      connection = new Socket ( InetAddress.getByName ( host ), port );

      outStream = connection.getOutputStream ();
      outDataStream = new DataOutputStream ( outStream );
      inStream = connection.getInputStream ();
      inDataStream = new DataInputStream ( inStream );

      logDisplay.appendText ( "Socket created:  connecting to server\n" );

      message = msgDisplay.getText ();
      outDataStream.writeUTF ( message );
      logDisplay.appendText ( "Message, below, sent to Server \n" );

      try {
        msgDisplay.setText ( "" );
        msgDisplay.setForeground ( Color.red );
        while ( true ) {
            message = inDataStream.readUTF ();
            msgDisplay.appendText ( message );
            logDisplay.appendText ( "Message, below, received from server\n" );
        }  // end while
        }  // end try for input
      catch ( EOFException except ) {
            connection.close ();
            logDisplay.appendText ( "Connection closed\n" );
        }  // end catch
      catch ( IOException except ) {
        System.out.println ( "IO Exception");
        except.printStackTrace ();
        }  // end catch

    }  // end try

    catch ( IOException except)  {
      except.printStackTrace ();
    }  // end catch

  }  // end connectClient

// **************


  //***********  Utility Methods   ***********


// **************

  public void buildUI ()  {

    hostDisplay = new TextField ( DEFAULT_HOST, 30 );
    portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
    topPanel = new Panel ();
    topPanel.setLayout ( new GridLayout ( 2, 1 ) );
    topPanel.add ( hostDisplay );
    topPanel.add ( portDisplay );

    logDisplay = new TextArea ( 40, 10 );
    msgDisplay = new TextArea ( 40, 10 );
    msgDisplay.setText ( "Default Message." );
    middlePanel = new Panel ();
    middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
    middlePanel.add ( logDisplay );
    middlePanel.add ( msgDisplay );

    sendButton = new Button ( "Send" );
    quitButton = new Button ( "Quit" );
    sendButton.addActionListener ( this );
    quitButton.addActionListener ( this );
    buttonPanel = new Panel ( );
    buttonPanel.add ( sendButton );
    buttonPanel.add ( quitButton );

    outerBox = new MyFrame (  );
    outerBox.addWindowListener ( outerBox );

    outerBox.add ( "North", topPanel );
    outerBox.add ( "Center", middlePanel );
    outerBox.add ( "South", buttonPanel );

    outerBox.resize ( 400, 450 );
    outerBox.show ();

  }  // end buildUI


  //***********  Interface Methods   ***********


  //****  ActionListener method

  //****  ActionListener methods

  public void actionPerformed ( ActionEvent e )  {

    Object s = e.getSource();

    // *** process Button actions

    if ( s instanceof Button )  {

        if ( s == sendButton )  {
            connectClient ();
        }  // end sendButton

        if ( s == quitButton )  {
            outerBox.hide ();
            outerBox.dispose ();
            System.exit ( 0 );
        }  // end quitButton

    }  // end process Button actions

  }  // end actionPerformed


  //****  WindowListener methods

  public void windowActivated ( WindowEvent e )  {
  }

  public void windowDeactivated ( WindowEvent e )  {
  }

  public void windowOpened ( WindowEvent e )  {
  }

   public void windowClosed ( WindowEvent e )  {
  }

  public void windowClosing ( WindowEvent e )  {
    outerBox.hide ();
    outerBox.dispose ();
    System.exit(0);
  }

  public void windowIconified ( WindowEvent e )  {
  }

  public void windowDeiconified ( WindowEvent e )  {
  }


}  // end Client_Applet

Run the applet