(java)Cara Mengetahui IP menggunakan InetAddress

Di karenakan saya lagi proses pembuatan App Android dan bermain main dengan IP static, pertama saya mencari bagaimana menulis atau mendapatkan ip menggunkan java desktop, di java desktop kita dapat menggukana aktivitas InetAddress untuk mengetahui ip static.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ipadress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 *
 * @author mrroot
 */
public class IpAddressweb {
    public static void main(String[] args) throws IOException {
        String nama;
        BufferedReader br;
        br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Masukkan Alamat URL :");
        nama = br.readLine();
        try {
            InetAddress address = InetAddress.getByName(nama);
            System.out.println(address);
        } catch (Exception e) {
            System.out.println("tidak ada ip atau URL tidak terdaftar");
        }
    }
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ipadress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 *
 * @author mrroot
 */
public class IpAddress2 {
    public static void main(String[] args) throws IOException {
        String nama;

      BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
      System.out.print("Masukkan URL : ");
      nama = br.readLine();
    try
    {
        InetAddress address[] = InetAddress.getAllByName(nama);
        for(int i=0;i<address.length;i++)System.out.println(address[i]);
    //System.out.println(address);
    }
    catch (UnknownHostException e)
    {
    System.out.println("URL TIdak Terdaftar");
    }
  }
}

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s