我有 DNS 服务器的 IP 地址和主机名。
使用 Java,我怎样才能找到由该 DNS 服务器返回的 IP 地址和主机名?
Take a look at InetAddress and the getHostAddress() method.
InetAddress
getHostAddress()
InetAddress address = InetAddress.getByName("www.example.com"); System.out.println(address.getHostAddress());
You can use InetAddress for this. Try the below code,
InetAddress address = InetAddress.getByName("www.yahoo.com"); System.out.println(address.getHostAddress()); System.out.println(address.getHostName());
You can do it like this:
for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com")) System.out.println(addr.getHostAddress());
As suggested by all above, you can use InetAddress.getByName("hostName") but this can give you a cached IP, Read the java documentation for the same. If you want to get a IP from DNS you can use:
InetAddress.getByName("hostName")
InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");