An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built. In Java, the InetAddress class represents an Internet Protocol (IP) address.

IP地址是IP使用的32位或128位无符号数字,IP是在其上构建UDP和TCP等协议的较低级别协议。 在Java中, InetAddress类表示Internet协议(IP)地址。

Here we will learn how to get localhost IP address and a website IP addresses in java using InetAddress.

在这里,我们将学习如何使用InetAddress在Java中获取本地主机IP地址和网站IP地址。

package com.journaldev.util;import java.net.UnknownHostException;
import java.net.InetAddress;public class JavaIPAddress {/*** @param args* @throws UnknownHostException */public static void main(String[] args) throws UnknownHostException {//print localhost ip addressSystem.out.println(InetAddress.getLocalHost().getHostAddress());//print website ip addressSystem.out.println(InetAddress.getByName("www.journaldev.com"));//print all ip addresses for a websiteInetAddress[] inetAddresses = InetAddress.getAllByName("www.google.com");for(InetAddress inet : inetAddresses){System.out.println(inet);}}}

Output of the above program is:

上面程序的输出是:

192.168.3.1
www.journaldev.com/50.116.65.160
www.google.com/74.125.224.82
www.google.com/74.125.224.81
www.google.com/74.125.224.80
www.google.com/74.125.224.83
www.google.com/74.125.224.84
www.google.com/2001:4860:4001:803:0:0:0:1010

When we use InetAddress.getByName(String host) it returns the current IPv4 address of the hostname, when we use InetAddress.getAllByName(String host) it returns all the IP addresses associated with the hostname. The last output of google.com IP is IPv6 format IP address.

当我们使用InetAddress.getByName(String host)它返回主机名的当前IPv4地址,当我们使用InetAddress.getAllByName(String host)它返回与主机名关联的所有IP地址。 google.com IP的最后输出是IPv6格式的IP地址。

These methods throw UnknownHostException if there are no IP addresses associated with the hostname. Note that we should not use any protocol like HTTP to provide the hostname input.

如果没有与主机名关联的IP地址,则这些方法将引发UnknownHostException 。 请注意,我们不应使用HTTP之类的协议来提供主机名输入。

翻译自: https://www.journaldev.com/735/how-to-get-ip-address-in-java-using-inetaddress

如何使用InetAddress在Java中获取IP地址相关推荐

  1. php 什么函数获取ip,在PHP中获取ip地址的方法有哪些

    在PHP中获取ip地址的方法有哪些 发布时间:2021-02-15 18:37:36 来源:亿速云 阅读:97 作者:Leah 这篇文章给大家介绍在PHP中获取ip地址的方法有哪些,内容非常详细,感兴 ...

  2. 通过JAVA自动获取Ip地址

    今天给大家提供一款自动获取Ip地址的工具类 一般在做电商项目都会使用到这个功能,所以小冰今天就直接放到这里,方便后期使用 代码如下: package net.xdclass.xdvideo.utils ...

  3. java 获取http地址_java如何获取当前时间,java如何获取ip地址

    展开全部 获取当前时间public static void main(String[] args) throws IOException { SimpleDateFormat format = new ...

  4. Java中获取内存地址

    在Python中,可以通过id()方法来获取变量所指向的内存空间的内存空间 class Bookstore:def __init__(self,Bookname,price):self.Booknam ...

  5. java获取ip地址不重复,如何在android(Java)中验证Ip地址[重复]

    看看下面的代码.除了有效性,它还检查它所属的IP地址类以及它是否是保留的IP地址 Pattern ipPattern; int[] arr=new int[4]; int i=0; //Method ...

  6. Java中获取MAC地址

    通过调用dos命令实现获取 import java.io.*; public class Test { public static void main(String[] args) { try { P ...

  7. java 动态获取IP地址(城市)

    import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...

  8. 【Java网络编程】获得实验室局域网中所有开机主机名称和IP地址,InetAddress类,在java中如何获取IP地址的方法

    获得实验室所有主机的IP地址和主机名 一.InetAddress类 二.获得指定IP地址和主机名的主要方法以及练习 1.主要方法 2.源码 三.获得实验室局域网中所有开机主机名称和IP地址 1.源码 ...

  9. 【更新】关于VMware虚拟机无法正常获取IP地址问题的解决方法及思路

    目录 一.先从一个示例入手 二.为什么会出现网卡无法正常获取IP地址的情况?这种时候该如何解决呢? 1. 原因 2. 解决方法 2.1. VMware程序出现bug 2.2. VMware的虚拟网络编 ...

最新文章

  1. 3D MinkowskiEngine稀疏模式重建
  2. ROS中使用摄像头的问题
  3. 说说软件开发这个职业
  4. Laravel 有哪些核心的内容?
  5. java mission 篡改后 网速很慢_关于直连网线速度正常,插上路由器后速度变慢的解决方法{不全面}...
  6. [云炬创业基础笔记]第七张创业资源测试11
  7. 08年哈弗校长Faust给毕业生的演讲
  8. 鸿蒙系统是单任务还是多任务,在鸿蒙系统上使用MQTT编程
  9. 关于win10更新后谷歌浏览器打开卡慢或者无法上网的问题解决
  10. AI ProCon倒计时6天:60+业内技术专家,探秘9大核心技术专题
  11. java查询到更新之前的数据_Java对数据库的查询和更新操作详解
  12. python连接不上数据库_绕不过去的Python连接MySQL数据库
  13. 单元格中指定内容标红_按照指定单元格内容进行拆分,想怎么拆就怎么拆
  14. 正式发布 .Net2.0 大文件上传服务器控件
  15. Android自定义控件及自定义属性
  16. inSSIDer 软件 WIFI信号强度测试
  17. 图片居中操作 空行 html,word中进行图片居中对齐批量设置的操作技巧
  18. MySQL生成测试数据相关脚本(持续更新)
  19. 教你认识正斜杠(/)与反斜杠(\)
  20. oracle revoke 列_oracle受权与回收权限grant和revoke

热门文章

  1. 发一个招聘软件开发人员的帖子
  2. [转载] Python round四舍五入精度缺失的解决
  3. [转载] python实现基本算法之插入排序(Insertion Sort)
  4. [转载] np.logical_and用法(有代码)
  5. [转载] 吴恩达机器学习逻辑回归练习题:逻辑回归及规则化(python实现)
  6. [转载] 【Python进阶】4-2 多态 | 什么是多态 / 静态语言vs动态语言 / python中多态
  7. spring缓存与Redis
  8. 后缀自动机(学习笔记)
  9. Searching the Web论文阅读
  10. NodsJs第六天笔记