InetAddress.getLocalHost() 如何运作?
How InetAddress.getLocalHost() works?
我正在尝试了解 InetAddress.getLocalHost()
的工作原理。 Javadoc 表示它从系统中检索主机名,然后将其解析为 InetAddress
。 "resolve into InetAddess" 到底是什么意思?它只是要求 DNS 解析主机名吗?
来自InetAddress.java
来源:
private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
throws UnknownHostException
{
InetAddress[] addresses = null;
boolean success = false;
UnknownHostException ex = null;
// Check whether the host is in the lookupTable.
// 1) If the host isn't in the lookupTable when
// checkLookupTable() is called, checkLookupTable()
// would add the host in the lookupTable and
// return null. So we will do the lookup.
// 2) If the host is in the lookupTable when
// checkLookupTable() is called, the current thread
// would be blocked until the host is removed
// from the lookupTable. Then this thread
// should try to look up the addressCache.
// i) if it found the addresses in the
// addressCache, checkLookupTable() would
// return the addresses.
// ii) if it didn't find the addresses in the
// addressCache for any reason,
// it should add the host in the
// lookupTable and return null so the
// following code would do a lookup itself.
...
if (host.equalsIgnoreCase("localhost")) {
InetAddress[] local = new InetAddress[] { impl.loopbackAddress() }; // {0x7f,0x00,0x00,0x01}
addresses = local;
success = true;
break;
}
回顾一下:
InetAddress.getAllByName()
和 InetAddress.getLocalHost()
都通过调用 getAddressesFromNameService()
来解析地址
- JVM 维护自己的主机名缓存 -> IP 地址映射。
- 如果地址不在缓存中(
lookupTable
或 addressCache
),它将调用操作系统的 DNS(具体行为可能因 JVM 实现而异)。
- 对于localhost sepcificly - 里面有一个特定的案例
getAddressesFromNameService()
我正在尝试了解 InetAddress.getLocalHost()
的工作原理。 Javadoc 表示它从系统中检索主机名,然后将其解析为 InetAddress
。 "resolve into InetAddess" 到底是什么意思?它只是要求 DNS 解析主机名吗?
来自InetAddress.java
来源:
private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
throws UnknownHostException
{
InetAddress[] addresses = null;
boolean success = false;
UnknownHostException ex = null;
// Check whether the host is in the lookupTable.
// 1) If the host isn't in the lookupTable when
// checkLookupTable() is called, checkLookupTable()
// would add the host in the lookupTable and
// return null. So we will do the lookup.
// 2) If the host is in the lookupTable when
// checkLookupTable() is called, the current thread
// would be blocked until the host is removed
// from the lookupTable. Then this thread
// should try to look up the addressCache.
// i) if it found the addresses in the
// addressCache, checkLookupTable() would
// return the addresses.
// ii) if it didn't find the addresses in the
// addressCache for any reason,
// it should add the host in the
// lookupTable and return null so the
// following code would do a lookup itself.
...
if (host.equalsIgnoreCase("localhost")) {
InetAddress[] local = new InetAddress[] { impl.loopbackAddress() }; // {0x7f,0x00,0x00,0x01}
addresses = local;
success = true;
break;
}
回顾一下:
InetAddress.getAllByName()
和InetAddress.getLocalHost()
都通过调用getAddressesFromNameService()
来解析地址
- JVM 维护自己的主机名缓存 -> IP 地址映射。
- 如果地址不在缓存中(
lookupTable
或addressCache
),它将调用操作系统的 DNS(具体行为可能因 JVM 实现而异)。 - 对于localhost sepcificly - 里面有一个特定的案例
getAddressesFromNameService()