Java Plug-in 1.3 and RSA Signed Applets
Pages: 1, 2, 3
The SimpleScannerApplet Source Code
Now that you know
what the scanner does, let's see how it works. Listing 1 shows the
source code of the SimpleScannerApplet. It extends the
Swing JApplet class and defines two inner classes:
Scanner and ScannerException. The
Scanner class handles the clicking of the Scan button and
performs the actual scanning. The ScannerException class
is used to throw scanner-related exceptions.
Listing 1. The SimpleScanner Applet
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
public class SimpleScannerApplet extends JApplet {
// Declare the GUI components used by the applet
JTextField hostTextField = new JTextField("localhost",20);
JTextField portTextField = new JTextField("80",5);
JTextField resultsTextField = new JTextField(40);
JButton scanButton = new JButton("Scan");
JLabel hostLabel = new JLabel("Host name or IP address: ");
JLabel portLabel = new JLabel("Port number: ");
JLabel resultsLabel = new JLabel("Results: ");
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
public SimpleScannerApplet() {
// Layout the applet's components
Border etched = BorderFactory.createEtchedBorder();
// Add an etched border to the main panel
mainPanel.setBorder(BorderFactory.createTitledBorder(etched, "Simple Port Scanner"));
// Host and port fields in top panel
topPanel.add(hostLabel);
topPanel.add(hostTextField);
topPanel.add(portLabel);
topPanel.add(portTextField);
// Scan button and result field in bottom panel
bottomPanel.add(scanButton);
bottomPanel.add(resultsLabel);
bottomPanel.add(resultsTextField);
// Add top and bottom panels to main panel
mainPanel.add(topPanel);
mainPanel.add(bottomPanel);
// Set up the button's event handler
scanButton.addActionListener(new Scanner());
// Add the main panel to the applet's content pane
getContentPane().add(mainPanel);
}
// The Scan button's event handler does the actual scanning
private class Scanner implements ActionListener {
// Declare fields used as scan parameters
String host;
InetAddress address;
int port;
String ip;
// Handle the button's action event
public void actionPerformed(ActionEvent ev) {
out("Checking scan parameters ...");
if(validParameters()) {
out("Scanning ...");
try {
// Try to connect to the host/port
Socket s = new Socket(address, port);
// No exception: SUCCESS
out("Port "+port+" is open on "+host+" ("+ip+").");
s.close();
}catch(Exception ex) {
// Could not connect
if(ex instanceof SecurityException) out(ex.getMessage());
else out("Port "+port+" is closed on "+host+" ("+ip+").");
}
}
}
// Validate scan parameters
private boolean validParameters() {
try {
host = hostTextField.getText();
String portString = portTextField.getText();
// Convert host name to an InetAddress object
address = InetAddress.getByName(host);
// Get the host's IP address
ip = address.getHostAddress();
// Convert the portString to an int
port = Integer.decode(portString).intValue();
// Make sure that it's in range
if(port > 65535) throw new ScannerException("Invalid port.");
}catch(Exception e) {
// Handle any validation-related exceptions
out(e.getMessage());
return false;
}
return true;
}
// Convenience method for writing results
private void out(String msg) {
resultsTextField.setText(msg);
}
}
// Used to identify invalid scan parameters
private class ScannerException extends Exception {
public ScannerException(String msg) {
super(msg);
}
}
}
SimpleScannerApplet
The
SimpleScannerApplet begins by declaring the GUI
components that it displays. It uses Swing components for maximum
compatibility across browser platforms.
// Declare the GUI components used by the applet
JTextField hostTextField = new JTextField("localhost",20);
JTextField portTextField = new JTextField("80",5);
JTextField resultsTextField = new JTextField(40);
JButton scanButton = new JButton("Scan");
JLabel hostLabel = new JLabel("Host name or IP address: ");
JLabel portLabel = new JLabel("Port number: ");
JLabel resultsLabel = new JLabel("Results: ");
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
The SimpleScannerApplet constructor lays out the
applet's GUI and creates an instance of Scanner to act as
the Scan button's event handler.
public SimpleScannerApplet() {
// Layout the applet's components
Border etched = BorderFactory.createEtchedBorder();
// Add an etched border to the main panel
mainPanel.setBorder(BorderFactory.createTitledBorder(etched,
"Simple Port Scanner"));
// Host and port fields in top panel
topPanel.add(hostLabel);
topPanel.add(hostTextField);
topPanel.add(portLabel);
topPanel.add(portTextField);
// Scan button and result field in bottom panel
bottomPanel.add(scanButton);
bottomPanel.add(resultsLabel);
bottomPanel.add(resultsTextField);
// Add top and bottom panels to main panel
mainPanel.add(topPanel);
mainPanel.add(bottomPanel);
// Set up the button's event handler
scanButton.addActionListener(new Scanner());
// Add the main panel to the applet's content pane
getContentPane().add(mainPanel);
}
Scanner
The Scanner inner class performs
the actual scanning. It begins by declaring field variables that store
the scan parameters.
// Declare fields used as scan parameters
String host;
InetAddress address;
int port;
String ip;
It then implements the actionPerformed() method of
java.awt.event.ActionListener to handle the clicking of
the Scan button. It uses the validParameters() method to
determine whether the user supplied a valid host name/IP address and
port number and to convert the users inputs into an
InetAddress object and int port value.
A try-catch statement is used to catch any exceptions
that result from the scanning. An attempt is made to create a TCP
socket to the specified address and port. If the attempt is
successful, then the port is identified as being open and then the
socket is closed. If the attempt is unsuccessful, then an exception is
thrown by the Socket constructor. The catch
block handles this exception by checking to see if the exception is a
SecurityException or some other type of exception.
If a SecurityException is thrown, then the message
associated with the exception is displayed in the Results
field. Otherwise, the port is identified as being closed for the
specified host/IP address.
// Handle the button's action event
public void actionPerformed(ActionEvent ev) {
out("Checking scan parameters ...");
if(validParameters()) {
out("Scanning ...");
try {
// Try to connect to the host/port
Socket s = new Socket(address, port);
// No exception: SUCCESS
out("Port "+port+" is open on "+host+" ("+ip+").");
s.close();
}catch(Exception ex) {
// Could not connect
if(ex instanceof SecurityException)
out(ex.getMessage());
else
out("Port "+port+" is closed on "
+host+" ("+ip+").");
}
}
}
The Scanner class defines two other methods that support the
scanning process. The validParameters() method checks to
see if the user has entered a valid host/IP address and port number
and then converts those values from String objects to an
InetAddress object and int port value.
// Validate scan parameters
private boolean validParameters() {
try {
host = hostTextField.getText();
String portString = portTextField.getText();
// Convert host name to an InetAddress object
address = InetAddress.getByName(host);
// Get the host's IP address
ip = address.getHostAddress();
// Convert the portString to an int
port = Integer.decode(portString).intValue();
// Make sure that it's in range
if(port > 65535)
throw new ScannerException("Invalid port.");
}catch(Exception e) {
// Handle any validation-related exceptions
out(e.getMessage());
return false;
}
return true;
}
The out() method simplifies the display of output to
the Results field.
// Convenience method for writing results
private void out(String msg) {
resultsTextField.setText(msg);
}
ScannerException
The ScannerException class extends
java.lang.Exception to provide a mechanism for
identifying scanner-related exceptions.
// Used to identify invalid scan parameters
private class ScannerException extends Exception {
public ScannerException(String msg) {
super(msg);
}
}