Berikut contoh program untuk mendapatkan alamat IP, kita akan meggunakan metode dibawah ini
- Mendapatkan alamat local host dengan memanggil metode getLocalHost() method dari InetAdress class
- Mendapatkan alamat IP dengan menggunakan getHostAddress() method.
berikut contoh programnya
Nama file : get_My_Ip.java
import java.net.InetAddress;
public class get_My_Ip {
public static void main(String args[]) throws Exception
{
/* public static InetAddress getLocalHost()
* throws UnknownHostException: Returns the address
* of the local host. This is achieved by retrieving
* the name of the host from the system, then resolving
* that name into an InetAddress. Note: The resolved
* address may be cached for a short period of time.
*/
InetAddress myIP=InetAddress.getLocalHost();
/* public String getHostAddress(): Returns the IP
* address string in textual presentation.
*/
System.out.println("My IP Address is:");
System.out.println(myIP.getHostAddress());
}
}
Output
run:
My IP Address is:
172.16.254.1
BUILD SUCCESSFUL (total time: 1 second)
Selamat mencoba, semoga bermanfaat ...
No comments:
Post a Comment