InetAddress.getLocalHost()
时间: 2023-06-21 21:03:53 浏览: 131
Android开发之InetAddress基础入门简介与源码实例
In Java, `InetAddress.getLocalHost()` is a method that returns an instance of the `InetAddress` class representing the local host. It essentially returns the IP address of the machine on which the code is running.
Here's an example:
```java
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LocalHostExample {
public static void main(String[] args) {
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("Localhost IP Address: " + localhost.getHostAddress());
System.out.println("Localhost Host Name: " + localhost.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
```
When you run this code, it will print out the IP address and host name of the local machine. Note that `getHostName()` may not always return the fully qualified domain name (FQDN) of the machine, depending on the configuration.
阅读全文