gpt4 book ai didi

java - 这段代码中何时发生 DNS 查找?通过 getByName() 或 getHostName() 或 getHostAddress()?

转载 作者:行者123 更新时间:2023-12-01 13:09:41 25 4
gpt4 key购买 nike

DNS 查找发生在哪一行?

import java.net.InetAddress;
import java.net.UnknownHostException;
public class Addresses {
public static void main(String[] args) {

try {
InetAddress address= InetAddress.getByName("www.sun.com"); //line 7
System.out.println(address.getHostName()+ "-"+address.getHostAddress());//line 8
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
<小时/>

据我了解应该是在第7行。但是在一组教程中我发现据说是在第8行。但是没有任何解释。如果它在第 8 行,但不在第 7 行,有人可以解释一下吗?

最佳答案

来自 JAVA 源 InetAddress.getByName("www.sun.com");和 address.getHostName() 进行查找。

InetAddress.getByName("www.sun.com");方法将在这里进行查找

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 ((addresses = checkLookupTable(host)) == null) {
try {
// This is the first thread which looks up the addresses
// this host or the cache entry for this host has been
// expired so this thread should do the lookup.
for (NameService nameService : nameServices) {
try {
/*
* Do not put the call to lookup() inside the
* constructor. if you do you will still be
* allocating space when the lookup fails.
*/

addresses = nameService.lookupAllHostAddr(host);
success = true;
break;
} catch (UnknownHostException uhe) {
if (host.equalsIgnoreCase("localhost")) {
InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
addresses = local;
success = true;
break;
}
else {
addresses = unknown_array;
success = false;
ex = uhe;
}
}
}

// More to do?
if (reqAddr != null && addresses.length > 1 && !addresses[0].equals(reqAddr)) {
// Find it?
int i = 1;
for (; i < addresses.length; i++) {
if (addresses[i].equals(reqAddr)) {
break;
}
}
// Rotate
if (i < addresses.length) {
InetAddress tmp, tmp2 = reqAddr;
for (int j = 0; j < i; j++) {
tmp = addresses[j];
addresses[j] = tmp2;
tmp2 = tmp;
}
addresses[i] = tmp2;
}
}
// Cache the address.
cacheAddresses(host, addresses, success);

if (!success && ex != null)
throw ex;

} finally {
// Delete host from the lookupTable and notify
// all threads waiting on the lookupTable monitor.
updateLookupTable(host);
}
}

return addresses;
}

address.getHostName() 方法在此处查找

 private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
for (NameService nameService : nameServices) {
try {
// first lookup the hostname
host = nameService.getHostByAddr(addr.getAddress());

/* check to see if calling code is allowed to know
* the hostname for this IP address, ie, connect to the host
*/
if (check) {
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
sec.checkConnect(host, -1);
}
}

/* now get all the IP addresses for this hostname,
* and make sure one of them matches the original IP
* address. We do this to try and prevent spoofing.
*/

InetAddress[] arr = InetAddress.getAllByName0(host, check);
boolean ok = false;

if(arr != null) {
for(int i = 0; !ok && i < arr.length; i++) {
ok = addr.equals(arr[i]);
}
}

//XXX: if it looks a spoof just return the address?
if (!ok) {
host = addr.getHostAddress();
return host;
}

break;

} catch (SecurityException e) {
host = addr.getHostAddress();
break;
} catch (UnknownHostException e) {
host = addr.getHostAddress();
// let next provider resolve the hostname
}
}

return host;
}

关于java - 这段代码中何时发生 DNS 查找?通过 getByName() 或 getHostName() 或 getHostAddress()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com