当想通过代码知道自己主机地址和主机名,或者查询网络上服务器地址时,我们都可以用InetAddress类。 java.net.InetAddress类是用来表示IP地址的高层表示,大多数有关于Java网络相关的类都和它有关系,例如socket,URL等。 下面我们用代码介绍InetAddress类: package com.net2; import java.net.InetAddress; import java.net.UnknownHostException ; public class Test1 { public static void main(String[] args) { try { InetAddress addr = InetAddress.getLocalHost ; System.out.println(); InetAddress address = InetAddress.getByName("DESKTOP-PVJBFQL");
InetAddress类 InetAddress类用来封装我们前面讨论的数字式的IP地址和该地址的域名。 你通过一个IP主机名与这个类发生作用,IP主机名比它的IP地址用起来更简便更容易理解。 InetAddress类内部隐藏了地址数字。 InetAddress类中的工厂方法 InetAddress类没有明显的构造函数。为生成一个InetAddress对象,必须运用一个可用的工厂方法。 对于InetAddress,三个方法:getLocalHost()、getByName()以及getAllByName()可以用来创建InetAddress的实例。 InetAddress对象的获取 InetAddress的构造函数不是公开的(public),所以需要通过它提供的静态方法来获取,有以下的方法: static InetAddress[] getAllByName InetAddress address=InetAddress.getByName(“www.baidu.com”); 注意到这些方法可能会抛出的异常。
InetAddress类就是封装了IPv4地址和IPv6地址,比较简单。这是muduo库中少有的值语义的类,所以继承的是copyable。实际上copyable只是强调可以拷贝,并没有实际意义。 InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6) { static_assert(offsetof(InetAddress , addr6_) == 0, "addr6_ offset 0"); static_assert(offsetof(InetAddress, addr_) == 0, "addr_ offset ::InetAddress(StringArg ip, uint16_t port, bool ipv6) { if (ipv6) { memZero(&addr6_, sizeof addr6 ::resolve(StringArg hostname, InetAddress* out) { assert(out !
17.2.1 使用InetAddress Java提供了InetAddress类来代表IP地址,InetAddress下还有2个子类:Inet4Address、Inet6Address,它们分别代表 InetAddress类没有提供构造器,而是提供了如下两个静态方法来获取InetAddress实例: getByName(String host):根据主机获取对应的InetAddress对象。 InetAddress还提供了如下三个方法来获取InetAddress实例对应的IP地址和主机名: String getCanonicalHostName():获取此 IP 地址的全限定域名。 除此之外,InetAddress类还提供了一个getLocalHost()方法来获取本机IP地址对应的InetAddress实例。 根据原始IP地址来获取对应的InetAddress实例 InetAddress local = InetAddress.getByAddress(new byte[] {127,0,0,1});
创建InetAddress 对象 根据域名创建InetAddress对象 InetAddress addr1 = InetAddress.getByName(“www.baidu.com”); ] 如果获取不到主机名就返回ip 根据主机名创建InetAddress对象 InetAddress addr3 = InetAddress.getByName(“IFC-PCB-094 ] [HostAddress: 192.168.0.1] 根据域名和 ip字节数组创建 InetAddress 对象 InetAddress addr = InetAddress.getByAddress [] 对象 public static InetAddress[] getAllByName(String host) InetAddress[] addr = InetAddress.getAllByName ("www.baidu.com"); for (InetAddress inetAddress : addr) { System.out.println(inetAddress); } 输出:
创建InetAddress 对象 根据域名创建InetAddress对象 InetAddress addr1 = InetAddress.getByName("www.baidu.com"); [HostName : www.baidu.com] [HostAddress: 220.181.112.244] 根据ip创建InetAddress对象 InetAddress addr2 = InetAddress.getByName 对象 InetAddress addr3 = InetAddress.getByName("IFC-PCB-094"); [HostName : IFC-PCB-094] [HostAddress InetAddress 对象 public static InetAddress getByAddress(byte[] addr) InetAddress addr = InetAddress.getByAddress (String host) InetAddress[] addr = InetAddress.getAllByName("www.baidu.com"); for (InetAddress inetAddress
对象 InetAddress ip; try { ip = InetAddress.getByName("www.baidu.com"); // 获取该 InetAddress 实例的 localHost = InetAddress.getLocalHost(); System.out.println("本机 IP 地址所对应的 InetAddress 实例:"+localHost 】 Java 提供了 InetAddress 类代表 IP 地址,InetAddress 下还有两个子类 Inet4Address 和 Inet6Address,它们分别代表 IPv4 和 IPv6 InetAddress 类没有提供构造方法,而是提供了静态方法来获取 InetAddress 实例。 InetAddress getLocalHost() 获取本机 IP 地址所对应的 InetAddress 实例 public String getHostAddress() 返回该 InetAddress
InetAddress的用法 下面这个程序利用InetAddress.getByName()来得到你的和百度IP地址。 class TestMark_to_win { public static void main(String[] args) throws Exception { /* static InetAddress getByName(String host) Determines the IP address of a host, given the host's name. */ InetAddress a = InetAddress.getByName("localhost"); System.out.println(a.getHostAddress()); System.out.println (a.getHostName()); InetAddress b = InetAddress.getByName("www.baidu.com"); System.out.println
Java提供了InetAddress类来代表IP地址,InetAddress下还有两个子类:Inet4Address、Inet6Address,它们分别代表Internet Protocol version InetAddress类的方法详解: boolean isMulticastAddress():检查 InetAddress 是否为 IP 多播地址的实用程序例程。 static InetAddress getByAddress(String host, byte[] addr):根据提供的主机名和 IP 地址创建 InetAddress。 static InetAddress getByAddress(byte[] addr):返回给定原始 IP 地址的InetAddress对象。 static InetAddress getLocalHost():返回本地主机的地址。 这是通过从系统检索主机的名称,然后将该名称解析为InetAddress 。
InetAddress类 一.InetAddress类: InetAdderss类是JDK中提供了一个类,该类用于封装一个IP地址,并提供了一系列与IP地址相关的方法。 通过InetAddress对象便可获取指定主机名,IP地址等,接下来通过一个案例来演示InetAddress的常用方法,如下所示。 void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.getByName static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.getByName ) throws Exception { InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
获取地址 获取Internet上主机的地址 可以使用InetAddress上主机的静态方法: getByName(String s); 将一个域名或IP地址传递给该方法的参数s,获得一个InetAddress 类 { public static void main(String args[]){ try{ InetAddress address_1 = InetAddress.getByName (""); System.out.println(address_1.toString()); InetAddress address_2 = InetAddress.getByName 类中还有两个实例方法: public String getHostName():获取InetAddress对象所含的域名 public String getHostAddress():获取InetAddress 对象所含的IP地址 获取本地机的地址 使用InetAddress类的静态方法getLocalHost()获得一个InetAddress对象,该对象含有本地机的域名和IP地址。
InetAddress类的使用 package com.inetaddress; import java.net.InetAddress; import java.net.UnknownHostException 获取InetAddress类的方式: * 1)getByName(String host):通过主机(IP地址)获取(掌握) * 2)getLocalHost():获取本机的InetAddress对象( { @Test public void test2() { try { InetAddress inet = InetAddress.getByName(" addr1 = InetAddress.getByName("192.168.10.1"); System.out.println(addr1); InetAddress addr2 = InetAddress.getByName("www.baidu.com"); System.out.println(addr2); //InetAddress addr3 = InetAddress.getByName
背景介绍某次在 SpringBoot 2.2.0 项目的一个配置类中引入了这么一行代码:InetAddress.getLocalHost().getHostAddress()导致项目启动明显变慢。 同时报出了相关的警告信息: 2022-10-03 23:32:01.806 [TID: N/A] WARN [main] o.s.b.StartupInfoLogger - InetAddress.getLocalHost github.com/apple-oss-distributions/mDNSResponder/tree/mDNSResponder-1096.100.3实际多次测试发现,主机信息都在第三次发送网络包后返回(阻塞在 InetAddress.getLocalHost 那我们试试用 Arthas 的 profiler 命令生成一下火焰图吧:可以看到很多编译相关的,我们忽略之,只把主机信息获取的那部分放大:哦吼,时间基本都耗在了 InetAddress.getAddressesFromNameService
下面列出了 InetAddress 类常用的方法: 序号 方法描述 1 static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 2 static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。 (); try { //根据域名获取 InetAddress address1 = inetAddress.getInetAddress( //查看是否是回环地址 InetAddress address5 = inetAddress.getInetAddressByIp("127.0.0.1"); InetAddress.getByAddress(host); } public InetAddress getInetAddress(String host,byte[] ipByte
package com.immooc; /* * InetAddress类 */ import java.net.InetAddress; import java.net.UnknownHostException String[] args) throws UnknownHostException { // TODO Auto-generated method stub //获取本机的InetAddress 实例 InetAddress address =InetAddress.getLocalHost(); System.out.println("计算机名:"+address.getHostName
【背景】 在一次问题排查过程中,发现偶现调用"InetAddress.getByName()"无法通过域名解析到IP(实际在容器中都能正确解析到),因此怀疑和容器的DNS解析有问题。 相关代码如下所示: // InetAddress.java public static InetAddress getByName(String host) throws UnknownHostException { return InetAddress.getAllByName(host)[0]; } public static InetAddress[] getAllByName(String host [] getAllByName(String host, InetAddress reqAddr) throws UnknownHostException { ... , InetAddress reqAddr, boolean check) throws UnknownHostException { ...
在Java中InetAddress和InetSocketAddress看起来很相似,用来描述IP地址和主机名称。 重要的是InetSocketAddress包含InetAddress。 这意味着,如果我们想对InetSocketAddress中的InetAddress做任何操作,只需要通过getInetAddress()方法获得即可。 对照表 属性 InetAddress InetSocketAddress 描述对象 IP地址 Socket地址(IP地址+端口) 描述 IP和主机对象名称 IP和主机的对象名称,并包括端口号 解决问题 IP到主机名称,主机名称到IP IP到主机名称,主机名称到IP,可以包含端口 获取对象 InetAddress.getLocalhost(); InetAddress.getByName(String
这两个类都是InetAddress的子类。由于InetAddress没有public的构造方法,因此,要想创建InetAddress对象,必须得依靠它的四个静态方法。 InetAddress可以通过getLocalHost方法得到本机的InetAddress对象,也可以通过getByName、getAllByName和getByAddress得到远程主机的InetAddress 如果使用IP地址作为参数,输出InetAddress对象时域名为空(除非调用getHostName方法后,再输出InetAddress对象。getHostName方法将在下面的内容介绍)。 addresses[] = InetAddress.getAllByName(host); for (InetAddress address : addresses) address1 = InetAddress.getByAddress(ip); InetAddress address2 = InetAddress.getByAddress(“Oracle
InetAddress类 Java.net包中有InetAddress类的定义,InetAddress类的对象用于IP地址和域名,该类提供以下方法: getByName(String s):获得一个InetAddress 对象的域名; String getHostAddress():获取InetAddress对象的IP地址; getLocalHost():获得一个InetAddress对象,该对象含有本地机的域名和IP地址 【例 13-1】 说明Inetaddress类的用法的应用程序。程序演示如何获取www.XXX.net的域名和IP地址。 .*; Class Example10_1{ Public static void main(String args[]){ Try{ //以下代码通过域名建立InetAddress 对象: InetAddress addr = InetAddress.getByname(“www.XXX.net”); String domainName
1、InetAddress.isReachable()方法 public static final boolean isNodeReachable(String hostname) { try { InetAddress address = InetAddress.getByName(hostname); if (address.isReachable