2013年7月14日 星期日

[ Java 代碼範本 ] How do I use a host name to look up an IP address

來源自 這裡
Preface:
The InetAddress class can be used to perform Domain Name Server (DNS) lookups. For example, you can call the static InetAddress.getByName("www.teamcakes.com")to retrieve an InetAddress object for 'www.teamcakes.com'. This object would contain the canonical name, host name, and ip address of 'www.teamcakes.com'.

The DnsTest class below demonstrates InetAddress.getLocalHost(), which obtains an Internet Address for your local host. It also demonstratesInetAddress.getByName("www.google.com"), which gives an Internet Address object for 'www.google.com'. However, it should be noted that a DNS name can map to multiple servers, and the InetAddress.getAllByName("www.google.com") call retrieves an array of InetAddress objects that represent all of the 'www.google.com' servers retrieved from DNS.

DnsTest 範例代碼:
  1. package test;  
  2.   
  3. import java.net.InetAddress;  
  4. import java.net.UnknownHostException;  
  5.   
  6. public class DnsTest {  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             InetAddress inetAddress = InetAddress.getLocalHost();  
  10.             displayStuff("local host", inetAddress);  
  11.             System.out.print("--------------------------");  
  12.             inetAddress = InetAddress.getByName("www.google.com");  
  13.             displayStuff("www.google.com", inetAddress);  
  14.             System.out.print("--------------------------");  
  15.             InetAddress[] inetAddressArray = InetAddress.getAllByName("www.google.com");  
  16.             for (int i = 0; i < inetAddressArray.length; i++) {  
  17.                 displayStuff("www.google.com #" + (i + 1), inetAddressArray[i]);  
  18.             }  
  19.         } catch (UnknownHostException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.   
  24.     public static void displayStuff(String whichHost, InetAddress inetAddress) {  
  25.         System.out.println("--------------------------");  
  26.         System.out.println("Which Host:" + whichHost);  
  27.         System.out.println("Canonical Host Name:" + inetAddress.getCanonicalHostName());  
  28.         System.out.println("Host Name:" + inetAddress.getHostName());  
  29.         System.out.println("Host Address:" + inetAddress.getHostAddress());  
  30.     }  
  31. }  
輸出結果:
--------------------------
Which Host:local host
Canonical Host Name:John-Lee
Host Name:John-Lee
Host Address:192.168.6.1
----------------------------------------------------
Which Host:www.google.com
Canonical Host Name:tf-in-f99.1e100.net
Host Name:www.google.com
Host Address:173.194.72.99
----------------------------------------------------
Which Host:www.google.com #1
Canonical Host Name:tf-in-f99.1e100.net
Host Name:www.google.com
Host Address:173.194.72.99
--------------------------
...
--------------------------
Which Host:www.google.com #6
Canonical Host Name:tf-in-f105.1e100.net
Host Name:www.google.com
Host Address:173.194.72.105
--------------------------
Which Host:www.google.com #7
Canonical Host Name:2404:6800:4008:c01:0:0:0:6a
Host Name:www.google.com
Host Address:2404:6800:4008:c01:0:0:0:6a

This message was edited 2 times. Last update was at 15/07/2013 10:24:50

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...