Esempi Java - accesso all'indirizzo IP e nome host locale
L'esempio seguente mostra come utilizzare InetAddress classe getLocalAddress () per ottenere l'indirizzo IP locale e l'hostname:
/* 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); } }
Il codice di cui sopra viene eseguito in uscita è:
Local HostAddress: 192.168.1.4 Local host name: harsh