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

Java example - Get the local IP address and host name

黄舟
Release: 2017-01-20 11:36:05
Original
1596 people have browsed it

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);
   }
Copy after login

The output result of the above code is:

Local HostAddress: 192.168.1.4Local host name: harsh
Copy after login


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



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template