The following example demonstrates how to use the getLocalAddress() method of the InetAddress class to obtain the local IP address and host name:
/* 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); }
The output result of the above code is: The above is the Java example - obtain the content of the local IP address and host name. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)! Local HostAddress: 192.168.1.4Local host name: harsh