Java-通过IP地址获得域名和主机名
昨天停電,今天補(bǔ)上!!
今天換個(gè)方式貼,總感覺(jué)之前那樣不太好
如何通過(guò)IP地址獲得域名和主機(jī)名?
過(guò)程是這樣的
1.先將IP地址轉(zhuǎn)換為字節(jié)數(shù)組
2.通過(guò)InetAddress類(lèi)的getByAddress()方法,獲得網(wǎng)絡(luò)主機(jī)中具有指定IP地址的InetAddress對(duì)象
3.調(diào)用InetAddress對(duì)象的getCanonicalHostName()方法,獲得對(duì)應(yīng)的域名
4.通過(guò)getHostName()方法,獲得主機(jī)名
以下是所有的屬性
<span style="white-space:pre"> </span>public static JLabel label_ip;public static JLabel label_domain;public static JLabel label_host;//三個(gè)文本域public static JTextField tf_ip;public static JTextField tf_domain; public static JTextField tf_host;//兩個(gè)按鈕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地址轉(zhuǎn)換為字符串?dāng)?shù)組byte[] ipBytes=new byte[4]; //聲明存儲(chǔ)轉(zhuǎn)換后IP地址的字節(jié)數(shù)組for (int i = 0; i < 4; i++) {int m=Integer.parseInt(ipStr[i]); //轉(zhuǎn)換為整數(shù)byte b=(byte)(m&0xff); //轉(zhuǎn)換為字節(jié)ipBytes[i]=b;}InetAddress inetAddr=InetAddress.getByAddress(ipBytes); //創(chuàng)建InetAddress對(duì)象String canonical=inetAddr.getCanonicalHostName(); //獲取域名String host=inetAddr.getHostName(); //獲取主機(jī)名tf_domain.setText(canonical); //在文本框中顯示域名 tf_host.setText(host); //在文本框中顯示主機(jī)名} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}
這兩天一直在用InetAddress這個(gè)類(lèi)
以下有一個(gè)博客可以借鑒以下?http://www.cnblogs.com/hnrainll/archive/2012/01/09/2317515.html
最為重要的一句話(huà)是
<strong>InetAddress的實(shí)例對(duì)象包含以數(shù)字形式保存的IP地址,同時(shí)還可能包含主機(jī)名(如果使用主機(jī)名來(lái)獲取InetAddress的實(shí)例,或者使用數(shù)字來(lái)構(gòu)造,</strong> <strong>并且啟用了反向主機(jī)名解析的功能)。InetAddress類(lèi)提供了將主機(jī)名解析為IP地址(或反之)的方法。</strong> <strong>(官方文檔中也有類(lèi)似的解釋)</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
主機(jī)名解析
主機(jī)名到 IP 地址的解析?通過(guò)使用本地機(jī)器配置信息和網(wǎng)絡(luò)命名服務(wù)(如域名系統(tǒng)(Domain Name System,DNS)和網(wǎng)絡(luò)信息服務(wù)(Network Information Service,NIS))來(lái)實(shí)現(xiàn)。要使用的特定命名服務(wù)默認(rèn)情況下是本地機(jī)器配置的那個(gè)。對(duì)于任何主機(jī)名稱(chēng),都返回其相應(yīng)的 IP 地址。反向名稱(chēng)解析?意味著對(duì)于任何 IP 地址,都返回與 IP 地址關(guān)聯(lián)的主機(jī)。
InetAddress 類(lèi)提供將主機(jī)名解析為其 IP 地址(或反之)的方法。
今天精力好,我們一起來(lái)好好學(xué)習(xí)一下這個(gè)IP
首先我們知道百度 ?(www.baidu.com) ?眾所周知
1.www
萬(wàn)維網(wǎng)(亦作“Web”、“WWW”、“'W3'”,英文全稱(chēng)為“World Wide Web”),是一個(gè)由許多互相鏈接的超文本組成的系統(tǒng),通過(guò)互聯(lián)網(wǎng)訪(fǎng)問(wèn)。
這一句足夠了,超文本說(shuō)白了,就是你現(xiàn)在所看到的網(wǎng)頁(yè)頁(yè)面文本
2.baidu
自己命明
3.com
以com為結(jié)尾的是頂級(jí)域名
www.baidu.com ? 整個(gè)稱(chēng)之為域名,為什么要有域名,很簡(jiǎn)單為了方便人們記憶,誰(shuí)愿意天天去記IP地址。
而DNS解析 可以實(shí)現(xiàn)域名的解析從而得到對(duì)應(yīng)的IP地址
好像有些扯遠(yuǎn)了
我們所用的類(lèi)InetAddress中
getHostName
public String getHostName()如果此 InetAddress 是用主機(jī)名創(chuàng)建的,則記憶并返回主機(jī)名;否則,將執(zhí)行反向名稱(chēng)查找并基于系統(tǒng)配置的名稱(chēng)查找服務(wù)返回結(jié)果。如果需要查找名稱(chēng)服務(wù),則調(diào)用?getCanonicalHostName。
如果有安全管理器,則首先使用主機(jī)名和?-1?作為參數(shù)來(lái)調(diào)用其?checkConnect?方法,以查看是否允許該操作。如果不允許該操作,則其返回 IP 地址的文本表示形式。
這個(gè)說(shuō)明了當(dāng)我們NEW 了一個(gè)InetAddress類(lèi)時(shí),它會(huì)帶著主機(jī)名一起創(chuàng)建。
上面的CODE還用到了getByAddress()方法
getByAddress
public static InetAddress getByAddress(String?host,byte[]?addr)throws UnknownHostException主機(jī)名可以是機(jī)器名(如 "java.sun.com"),也可以是其 IP 地址的文本表示形式。
也不在主機(jī)名上執(zhí)行有效性檢查。
如果 addr 指定 IPv4 地址,則返回 Inet4Address 的實(shí)例;否則將返回 Inet6Address 的實(shí)例。
IPv4 地址 byte 數(shù)組的長(zhǎng)度必須為 4 個(gè)字節(jié),IPv6 byte 數(shù)組的長(zhǎng)度必須為 16 個(gè)字節(jié)
參數(shù) :addr -網(wǎng)絡(luò)字節(jié)順序的原始地址
返回:根據(jù)原始IP地址 創(chuàng)建的InetAddress對(duì)象
附上完整CODE
總結(jié)
以上是生活随笔為你收集整理的Java-通过IP地址获得域名和主机名的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: #include指令引号与尖括号的区别
- 下一篇: 针对网络脆弱性的攻击图分析方法总结