Home >Java >javaTutorial >Java example - Get the local IP address and host name

Java example - Get the local IP address and host name

黄舟
黄舟Original
2017-01-20 11:36:051608browse

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:

Local HostAddress: 192.168.1.4Local host name: harsh


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)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn