Jumat, 28 Mei 2010

Getting IP Address and Host Name

Java InetAddress provides functions to obtain an IP address or host name of a computer. IP address or host name that was found does not depend on ip address and host name of the local computer, but also can be used to check the IP address and host name on the Internet, certainly on condition that our computer is connected to the Internet. Use of this function is also closely related to the DNS settings on the computer that we use.

Here is a sample program that shows the functionality of these :



  • Create a new NetBeans project
  • Add a JFrame Form
  • Complete with two fruit labels, 2 pieces textfield (txtIPAddress and txtNamaHost) and three fruit Button (btnCheckIPAddress, btnCheckHostName and btnCheckLokal). See examples in the following figures :


  • Type the following code on btnCheckHostName, event actionPerformed :
    01private void btnCheckHostNameActionPerformed(java.awt.event.ActionEvent evt) {
    02 try {
    03 String strHostName = InetAddress.getByName(txtIPAddress.getText()).getHostName();
    04 JOptionPane.showMessageDialog(null, "Host name dari IP Address '" + txtIPAddress.getText() +"' = "+ strHostName);
    05 } catch (UnknownHostException ex) {
    06 JOptionPane.showMessageDialog(null, ex);
    07 Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);
    08
    09 }
    10
    11 }
  • Perform the necessary import libraries (import java.net.InetAddress; import javax.swing.JOptionPane;)
  • Type the following code on btnCheckIPAddress, event actionPerformed :
    01private void btnCheckIPAddressActionPerformed(java.awt.event.ActionEvent evt) {
    02
    03 try {
    04 String strIPAddress = InetAddress.getByName(txtHostName.getText()).getHostAddress() ;
    05 JOptionPane.showMessageDialog(null, "Alamat IP dari '"+txtHostName.getText() +"' ="+ strIPAddress);
    06 } catch (UnknownHostException ex) {
    07 JOptionPane.showMessageDialog(null, ex);
    08 Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);
    09
    10 }
    11 }
  • Type the following code in the actionPerformed event btnCheckLokal :
    01private void btnCheckLocalActionPerformed(java.awt.event.ActionEvent evt) {
    02 try {
    03 InetAddress AlamatInternet = InetAddress.getLocalHost();
    04 JOptionPane.showMessageDialog(null, "Host name lokal : " +AlamatInternet.getHostName());
    05 JOptionPane.showMessageDialog(null, "IP Address lokal : " +AlamatInternet.getHostAddress() );
    06 } catch (UnknownHostException ex) {
    07 JOptionPane.showMessageDialog(null, ex);
    08 Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);
    09
    10 }
    11 }
  • Save and run the application (SHIFT + F6). Enter the IP Address and click on "Check Host Name" or enter the host name and click "Check IP Address". Check Local Host IP and not require an entry. Here are some screenshots of messages that appear :






This program is not smart if it detects an input IP address or host name. For convenience, I use two pieces textfield as an example. In applied applications, input the IP Address or Host Name can be obtained through a variable.

1 komentar: