Java中的IP对象以及本地域名解析

时间:2023-03-09 13:36:47
Java中的IP对象以及本地域名解析

本地域名解析操作步骤:

1.打开C:\WINDOWS\system32\drivers\etc目录

2.找到host文件,用记事本打开

3.添加“空间IP  域名”

 package WebProgramingDemo;

 import java.io.IOException;
import java.net.InetAddress; public class IpDemo { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { InetAddress ip = InetAddress.getByName("www.sina.com.cn");
// 实现原理:调用了getAllByName(host)方法
/*
* public static InetAddress getByName(String host) throws
* UnknownHostException { return InetAddress.getAllByName(host)[0]; }
*/
// 返回 IP 地址字符串(以文本表现形式)。
System.out.println("addr:" + ip.getHostAddress());
// 获取此 IP 地址的主机名。
System.out.println("name:" + ip.getHostName());
// 本地域名解析:
// C:\Windows\System32\drivers\etc\hosts文件存放的就是本地域名解析文件
// 配置该文件的内容,可以实现屏蔽某些网站的功能,360网站屏蔽网站的原理也是这样,
// 它将这些网站对应的本地域名解析配置成:127.0.0.1
} }