迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

如何在 Java 中查找本地主机的 IP 地址

作者:迹忆客 最近更新:2023/02/06 浏览次数:

Java 网络 API 提供了一种使用 java.net 从 Java 程序中查找本地主机 IP 地址的方法。 InetAddress 类。 在 Java 程序中很少需要本地主机的 IP 地址。 大多数情况下,我使用 Unix 命令来查找本地主机的 IP 地址。 对于程序不需要 IP 地址但我们需要解决任何网络问题的所有实际用途,请使用 DOS 或 Windows 命令或使用 Linux 命令。 最近我的一个朋友在核心 Java 面试中遇到了这个问题,他们期望 Java 开发人员具有一些套接字编程经验,但是直到你知道或者你已经完成之前很难回答这个基于事实的问题,这促使我们去写这篇文章。

在本 Java 教程中,我们将看到如何从 Java 程序中查找本地主机的 IP 地址。 顺便说一句,最好记住 Unix 网络命令列表,以解决 Unix 环境中与 Java 应用程序相关的任何网络问题。


来自 Java 程序的本地主机的 IP 地址

正如我所说,java.net 包中的 InetAddress 用于表示 Java 中的 IP 地址。 IP 地址是 32 位或 128 位的无符号数字,由 IP 协议使用,该协议是许多流行协议(如 TCP 和 UDP)的主干。

IP 地址有 IPv4 和 IPv6 两种,IP 地址与主机相关联,可以通过主机名解析过程找到主机。

主机名解析是通过结合本地机器配置和网络命名服务(例如 DNS(域名系统)和 NIS(网络信息服务))来执行的。 InetAddress 有一个方法来解析主机名和 IP 地址,反之亦然。 这是从 Java 程序中查找 IP 地址的完整代码示例。

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);
     
    }
 
}

上述代码输出结果如下

Java 程序的本地主机的 IP 地址

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Class fields and instance fields in Java

发布时间:2025/04/14 浏览次数:99 分类:Java

In this article, you will learn the basic terminology of Java programming language such as local variables, input parameters, class fields, and instance fields in Java. Local variables in Java Variables whose scope is bound to a block, meth

Class file editor in Java

发布时间:2025/04/14 浏览次数:158 分类:Java

In this article, we will discuss Java Class File Editor, a tool created in Java to edit Java compiled classes. We can decompile Java classes after creating them and view them, but we need a tool like Java Class File Editor to modify them. F

How to clear the console in Java

发布时间:2025/04/14 浏览次数:172 分类:Java

In this tutorial, we will look at two ways to clear the console screen in Java. We will learn how to execute Java clear screen commands at runtime through examples. Clearing the console using ANSI escape codes in Java We can use special cod

console.log in Java

发布时间:2025/04/14 浏览次数:197 分类:Java

This tutorial explains the function in Java console.log() and how to display logs to the console in Java. console.log() is a function in JavaScript that is used to display log messages to the browser console. There is no such message in Jav

Java Change Date Format

发布时间:2025/04/14 浏览次数:67 分类:Java

There are various options available for converting date string to date format. The methods mentioned below can bring the desired result. Let us understand the various ways from the following code block. import java.text.ParseException ; imp

Calendar date in YYYY-MM-DD format in Java

发布时间:2025/04/14 浏览次数:74 分类:Java

Java Date encapsulates the current time and date. The Date class does this with the help of two constructors - Date() and Date(long millisec) constructor. We use Date() the constructor to initialize the object with the current time and date

Implementing a min-max heap in Java

发布时间:2025/04/14 浏览次数:55 分类:Java

In this article, we will implement a max heap and a min heap using PriorityQueue the class. We will also demonstrate inserting and removing elements from the heap. Introduction to Min-Max Heap in Java Heap is a tree-based data structure, wh

Implementing a Min Heap in Java

发布时间:2025/04/14 浏览次数:198 分类:Java

A min heap is a heap in which every internal node is less than or equal to the value of its child nodes. We will see in the following points how to implement a min heap with and without using a library. Minimal heap implementation in Java w

Increasing the heap space in Java

发布时间:2025/04/14 浏览次数:192 分类:Java

In Java, the heap space is mainly used for garbage collection and allocating memory for objects. A default heap space is allocated when JVM is installed on our machine, but it may be different. The following points show how we can increase

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便