Tips Java : Getting Address's IP and Host Name

by Son Rokhaniawan Perdata, S.T | 11:35 PM in , |

Java provides InetAddress's function to get host's internet protocol address and also name a computer. host's internet protocol address and also name that is gotten don't cling to host's internet protocol address and name local computer only, but can also be utilized for mengecek host's internet protocol address and name at Internet, obviously by condition of computer we most link with Internet. This logistic purpose also relates hand in glove with setting DNS on computer that we utilizes.

Following is sample program that points out fungsionalitas that:
  1. Make one project is Netbeans new

  2. Add one JFrame Form

  3. Provede with 2 tags, 2 numbers TextField (txtIPAddress dan txtNamaHost) and 3 Button (btnCheckIPAddress, btnCheckHostName dan btnCheckLokal). See example on pictured following:

  4. Following code cannikin type on btnCheckHostName, event actionPerformed

    1. private void btnCheckHostNameActionPerformed(java.awt.event.ActionEvent evt) {

    2. try {

    3. String strHostName = InetAddress.getByName(txtIPAddress.getText()).getHostName();

    4. JOptionPane.showMessageDialog(null, "Host name dari IP Address '" + txtIPAddress.getText() +"' =
      "+ strHostName);

    5. } catch (UnknownHostException ex) {

    6. JOptionPane.showMessageDialog(null, ex);

    7. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    8. }


    9. }


  5. Do import library that needful (import java.net.InetAddress; import javax.swing.JOptionPane;)

  6. Following code cannikin type on btnCheckIPAddress, event actionPerformed


    1. private void btnCheckIPAddressActionPerformed(java.awt.event.ActionEvent evt) {


    2. try {

    3. String strIPAddress = InetAddress.getByName(txtHostName.getText()).getHostAddress() ;

    4. JOptionPane.showMessageDialog(null, "Alamat IP dari '"+txtHostName.getText() +"' ="+ strIPAddress);

    5. } catch (UnknownHostException ex) {

    6. JOptionPane.showMessageDialog(null, ex);

    7. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    8. }

    9. }


  7. Following code cannikin type on btnCheckLokal event actionPerformed

    1. private void btnCheckLocalActionPerformed(java.awt.event.ActionEvent evt) {

    2. try {

    3. InetAddress AlamatInternet = InetAddress.getLocalHost();

    4. JOptionPane.showMessageDialog(null, "Host name lokal : " +AlamatInternet.getHostName());

    5. JOptionPane.showMessageDialog(null, "IP Address lokal : " +AlamatInternet.getHostAddress() );

    6. } catch (UnknownHostException ex) {

    7. JOptionPane.showMessageDialog(null, ex);

    8. Logger.getLogger(frmIpAddress.class.getName()).log(Level.SEVERE, null, ex);


    9. }

    10. }


  8. Save and runs application (SHIFT + F6). Insert IP Address and click on “Check Host Name” or insert host name and click “Check IP Address”. Check IP and Host Local don't need entry. Following is umpteen screenshot message one that performs:



    This program not smart's ala detect entry what that internet protocol address or host name. To easy, I utilize 2 numbers TextField for example. On terapan's application, input IP Address or Host Name can thru get variable.

Related Posts by Categories



0 comments:

About Me

My photo
I am its person simple. Bad blood platitude. Directly az goes to aim target

Shoutbox