How to find the IP address of the local host in Java
Java Network API provides a way to find the IP address of local host from Java program using java.net. InetAddress
class. It is rare that you need the IP address of local host in Java program. Most of the time, I use Unix commands to find the IP address of local host. For all practical purposes where program does not require IP address but we need to solve any network problem, use DOS or Windows commands or use Linux commands. Recently one of my friend faced this question in core Java interview, they expect Java developers to have some experience in socket programming, but it is difficult to answer this fact based question until you know or you have done it, which prompted us to write this article.
In this Java tutorial, we will see how to find the IP address of the local host from within a Java program. By the way, it is always a good idea to remember the list of Unix network commands to troubleshoot any network issues related to Java applications in Unix environment.
The IP address of the local host from the Java program
As I said, the in the java.net package InetAddress
is used to represent IP addresses in Java. An IP address is a 32-bit or 128-bit unsigned number used by the IP protocol, which is the backbone of many popular protocols such as TCP and UDP.
There are two types of IP addresses, IPv4 and IPv6. An IP address is associated with a host, and the host can be found through the host name resolution process.
Hostname resolution is performed by combining the local machine configuration and network naming services such as DNS (Domain Name System) and NIS (Network Information Service). InetAddress
There is a method to resolve hostname to IP address and vice versa. Here is the complete code example to find IP address from Java program.
import java.net.UnknownHostException;
/**
* 用于查找本地主机 IP 地址的简单 Java 程序。 这个程序使用
* InetAddress 从 java.net 包中查找 IP 地址。
*
*/
public class IPTest {
public static void main(String args[]) throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
// 获取本地主机的 IP 地址 - getHostAddress 以文本格式返回 IP 地址
String ipAddress = addr.getHostAddress();
System.out.println("IP address of localhost from Java Program: " + ipAddress);
//Hostname
String hostname = addr.getHostName();
System.out.println("Name of hostname : " + hostname);
}
}
The output of the above code is as follows
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:58 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:
Do you understand JavaScript closures?
Publish Date:2025/02/21 Views:110 Category:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
Publish Date:2025/02/21 Views:183 Category:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
Publish Date:2025/02/21 Views:156 Category:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
Publish Date:2024/03/23 Views:89 Category:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。
如何检查 JavaScript 中的变量是否为字符串
Publish Date:2024/03/23 Views:159 Category:JavaScript
-
本教程介绍了如何在 JavaScript 中检查变量是否为字符串。
在 JavaScript 中扁平化一个数组
Publish Date:2024/03/23 Views:114 Category:JavaScript
-
本教程介绍了如何在 JavaScript 中扁平化一个数组。
在 JavaScript 中将浮点数转换为整数
Publish Date:2024/03/23 Views:75 Category:JavaScript
-
本教程列出了在 JavaScript 中将浮点数转换为整数的各种方法。
JavaScript POST
Publish Date:2024/03/23 Views:99 Category:JavaScript
-
本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。