Java-通过IP地址获得域名和主机名
昨天停電,今天補上!!
今天換個方式貼,總感覺之前那樣不太好
如何通過IP地址獲得域名和主機名?
過程是這樣的
1.先將IP地址轉換為字節數組
2.通過InetAddress類的getByAddress()方法,獲得網絡主機中具有指定IP地址的InetAddress對象
3.調用InetAddress對象的getCanonicalHostName()方法,獲得對應的域名
4.通過getHostName()方法,獲得主機名
以下是所有的屬性
<span style="white-space:pre"> </span>public static JLabel label_ip;public static JLabel label_domain;public static JLabel label_host;//三個文本域public static JTextField tf_ip;public static JTextField tf_domain; public static JTextField tf_host;//兩個按鈕public static JButton btn_ByIpGainDomain;public static JButton btn_exit;//public static JFrame fr;public static JPanel panel;try {<span style="white-space:pre"> </span>String ip=tf_ip.getText(); //IP地址String[] ipStr=ip.split("[.]"); //IP地址轉換為字符串數組byte[] ipBytes=new byte[4]; //聲明存儲轉換后IP地址的字節數組for (int i = 0; i < 4; i++) {int m=Integer.parseInt(ipStr[i]); //轉換為整數byte b=(byte)(m&0xff); //轉換為字節ipBytes[i]=b;}InetAddress inetAddr=InetAddress.getByAddress(ipBytes); //創建InetAddress對象String canonical=inetAddr.getCanonicalHostName(); //獲取域名String host=inetAddr.getHostName(); //獲取主機名tf_domain.setText(canonical); //在文本框中顯示域名 tf_host.setText(host); //在文本框中顯示主機名} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}
這兩天一直在用InetAddress這個類
以下有一個博客可以借鑒以下?http://www.cnblogs.com/hnrainll/archive/2012/01/09/2317515.html
最為重要的一句話是
<strong>InetAddress的實例對象包含以數字形式保存的IP地址,同時還可能包含主機名(如果使用主機名來獲取InetAddress的實例,或者使用數字來構造,</strong> <strong>并且啟用了反向主機名解析的功能)。InetAddress類提供了將主機名解析為IP地址(或反之)的方法。</strong> <strong>(官方文檔中也有類似的解釋)</strong>-
Host Name Resolution
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned.Reverse name resolution means that for any IP address, the host associated with the IP address is returned.
The InetAddress class provides methods to resolve host names to their IP addresses and vice versa.?
Translation
主機名解析
主機名到 IP 地址的解析?通過使用本地機器配置信息和網絡命名服務(如域名系統(Domain Name System,DNS)和網絡信息服務(Network Information Service,NIS))來實現。要使用的特定命名服務默認情況下是本地機器配置的那個。對于任何主機名稱,都返回其相應的 IP 地址。反向名稱解析?意味著對于任何 IP 地址,都返回與 IP 地址關聯的主機。
InetAddress 類提供將主機名解析為其 IP 地址(或反之)的方法。
今天精力好,我們一起來好好學習一下這個IP
首先我們知道百度 ?(www.baidu.com) ?眾所周知
1.www
萬維網(亦作“Web”、“WWW”、“'W3'”,英文全稱為“World Wide Web”),是一個由許多互相鏈接的超文本組成的系統,通過互聯網訪問。
這一句足夠了,超文本說白了,就是你現在所看到的網頁頁面文本
2.baidu
自己命明
3.com
以com為結尾的是頂級域名
www.baidu.com ? 整個稱之為域名,為什么要有域名,很簡單為了方便人們記憶,誰愿意天天去記IP地址。
而DNS解析 可以實現域名的解析從而得到對應的IP地址
好像有些扯遠了
我們所用的類InetAddress中
getHostName
public String getHostName()如果此 InetAddress 是用主機名創建的,則記憶并返回主機名;否則,將執行反向名稱查找并基于系統配置的名稱查找服務返回結果。如果需要查找名稱服務,則調用?getCanonicalHostName。
如果有安全管理器,則首先使用主機名和?-1?作為參數來調用其?checkConnect?方法,以查看是否允許該操作。如果不允許該操作,則其返回 IP 地址的文本表示形式。
這個說明了當我們NEW 了一個InetAddress類時,它會帶著主機名一起創建。
上面的CODE還用到了getByAddress()方法
getByAddress
public static InetAddress getByAddress(String?host,byte[]?addr)throws UnknownHostException主機名可以是機器名(如 "java.sun.com"),也可以是其 IP 地址的文本表示形式。
也不在主機名上執行有效性檢查。
如果 addr 指定 IPv4 地址,則返回 Inet4Address 的實例;否則將返回 Inet6Address 的實例。
IPv4 地址 byte 數組的長度必須為 4 個字節,IPv6 byte 數組的長度必須為 16 個字節
參數 :addr -網絡字節順序的原始地址
返回:根據原始IP地址 創建的InetAddress對象
附上完整CODE
總結
以上是生活随笔為你收集整理的Java-通过IP地址获得域名和主机名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: #include指令引号与尖括号的区别
- 下一篇: 针对网络脆弱性的攻击图分析方法总结