|
The Applet Support API provides support to resolve between internet host names and addresses. Applets can map IP address to host names and resolve host names to the IP addresses using the methods in the SASClient class. In this example, we develop an applet which performs the address and the name lookup.
Import the following packages.
|
import java.applet.*; import java.lang.*; import java.awt.*; import java.awt.event.*; import com.adventnet.snmp.snmp2.*; |
Define the required class and the static main function.
|
public class saslookup extends Applet implements ActionListener { |
Instantiate the SnmpAPI, SnmpSession, SASClient classes and other UI components.
|
SnmpAPI api; SnmpSession session; SASClient sasclient; TextArea text; TextField nameField, addressField, timeoutField; Button nameButton, addressButton; |
Construct the SASClient instance.
|
try { sasclient = new SASClient(this, false); } catch(SnmpException se) { text.append("Error : " + se.getMessage() + "\n"); } //SASClient is null if not connected to SAServer if(sasclient == null) { addressButton.setEnabled(false); addressButton.setEnabled(false); } |
Include the code to perform lookups.
The getHostAddress() and the getHostName() methods available in SASClient class maps IP address and host names. The getHostAddress() function resolves the host name to a dotted IP address with the help of SAS. The getHostName() method maps the dotted IP address to a fully qualified internet host name.
|
if(e.getActionCommand().equals("Address Lookup")) { int timeout = 0; // other codes go here try { String address = sasclient.getHostAddress(nameField.getText(), timeout); } catch (Exception ex) { } } else if(e.getActionCommand().equals("Name Lookup")) { int timeout = 0; try { String name = sasclient.getHostName(addressField.getText(), timeout); } catch (Exception ex) { } } |
Refer the source code for saslookup present in <examples/sasapps/saslookup.java> for more information.
Now, we can compile the source and load and view the applet. Refer GET, GETNEXT for a discussion on the different ways of loading the applet and different HTML files we need to create.
|