Java-通过IP地址获得域名和主机名

时间:2022-05-06 13:39:26

昨天停电,今天补上!!

今天换个方式贴,总感觉之前那样不太好


如何通过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 exception							e2.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()
获取此 IP 地址的主机名。

如果此 InetAddress 是用主机名创建的,则记忆并返回主机名;否则,将执行反向名称查找并基于系统配置的名称查找服务返回结果。如果需要查找名称服务,则调用 getCanonicalHostName

如果有安全管理器,则首先使用主机名和 -1 作为参数来调用其 checkConnect 方法,以查看是否允许该操作。如果不允许该操作,则其返回 IP 地址的文本表示形式。

返回:
此 IP 地址的主机名;如果安全检查不允许操作,则返回 IP 地址的文本表示形式。
另请参见:
getCanonicalHostName()SecurityManager.checkConnect(java.lang.String, int)

这个说明了当我们NEW 了一个InetAddress类时,它会带着主机名一起创建。



上面的CODE还用到了getByAddress()方法

getByAddress

public static InetAddress getByAddress(String host,
byte[] addr)
throws UnknownHostException
根据提供的主机名和 IP 地址创建 InetAddress。不检查名称服务的地址有效性。

主机名可以是机器名(如 "java.sun.com"),也可以是其 IP 地址的文本表示形式。

也不在主机名上执行有效性检查。

如果 addr 指定 IPv4 地址,则返回 Inet4Address 的实例;否则将返回 Inet6Address 的实例。

IPv4 地址 byte 数组的长度必须为 4 个字节,IPv6 byte 数组的长度必须为 16 个字节

参数:
host - 指定主机
addr - 网络字节顺序的原始 IP 地址
返回:
根据原始 IP 地址创建的 InetAddress 对象。
抛出:
UnknownHostException - 如果 IP 地址的长度非法
从以下版本开始:
1.4


参数 :addr -网络字节顺序的原始地址

返回:根据原始IP地址 创建的InetAddress对象



附上完整CODE

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;

import javax.naming.Context;
import javax.swing.*;
public class ByIpGainDomainFrame extends JFrame{
GetLocalHostIpFrame getLocalHostIpFrame;
//三个标签
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;
public static void main(String[] args){
fr=new JFrame("By ip gain the domain and host!");
panel=new JPanel();
label_domain=auto_Label(panel, label_domain,50,50,50,50,"域名:");
label_host=auto_Label(panel, label_host, 50, 100, 50, 50, "主机名:");
label_ip=auto_Label(panel, label_ip, 50, 150, 50, 50, "IP:");
tf_domain=auto_TextField(panel, tf_domain, 100, 50, 200, 50,null);
tf_host=auto_TextField(panel, tf_host, 100, 100, 200, 50, null);
tf_ip=auto_TextField(panel, tf_ip, 100, 150, 200, 50, null);

panel.setLayout(null);







auto_Button(panel,btn_ByIpGainDomain, 50, 300, 150, 50,"获取域名和主机名",1);
auto_Button(panel,btn_exit, 250, 300, 100, 50,"退出系统",0);


fr.add(panel);
fr.setSize(500, 500);
fr.setDefaultLookAndFeelDecorated(true);
fr.setVisible(true);
}

public static JLabel auto_Label(JPanel jPanel,JLabel label,int x,int y,int length,int width,String str){
label=new JLabel();
label.setText(str);
label.setBounds(x,y,length,width);
jPanel.add(label);
return label;
}
public static JTextField auto_TextField(JPanel jPanel,JTextField tf,int x,int y,int length,int width,String str){
tf=new JTextField();
tf.setText(str);
tf.setBounds(x,y,length,width);
jPanel.add(tf);
return tf;
}
private static void auto_Button(JPanel jPanel,JButton btn,int x,int y,int length,int width,String str,int choose){
btn=new JButton();
System.out.print("\n"+x+" "+y+" "+length+" "+width+" ");
btn.setText(str);
btn.setBounds(x,y,length,width);
jPanel.add(btn);
switch (choose) {
case 1:{
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {

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 exception
e2.printStackTrace();
}

}
});
}
break;

default:{
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
}
break;
}

}


}