Beispiele Java - Zugriff auf die lokale IP-Adresse und den Hostnamen
Das folgende Beispiel zeigt, wie InetAddress Klasse getLocalAddress () -Methode verwenden, um die lokale IP-Adresse und den Hostnamen zu erhalten:
/* author by w3cschool.cc Main.java */ import java.net.InetAddress; public class Main { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: "+addr.getHostAddress()); String hostname = addr.getHostName(); System.out.println("Local host name: "+hostname); } }
Der obige Code ausgeführt wird, ausgegeben wird:
Local HostAddress: 192.168.1.4 Local host name: harsh