当前位置:K88软件开发文章中心编程语言JavaJava01 → 文章内容

java.net.InetAddress类

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-4 7:54:04

-->

InetAddress类简介

InetAddress类是Java中用于描述IP地址的类。它在java.net包中。在Java中分别用Inet4Address和Inet6Address类来描述IPv4和IPv6的地址。这两个类都是InetAddress的子类。由于InetAddress没有public的构造方法,因此,要想创建InetAddress对象,必须得依靠它的四个静态方法。

InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。

InetAddress的四个静态方法

  • static InetAddress[] getAllByName(String host)
  • static InetAddress getByAddress(byte[] addr)
  • static InetAddress getByAddress(String host,byte[] addr)
  • static InetAddress getByName(String host)
  • static InetAddress getLocalHost()

注:使用这些方法会抛出出UnknowHostException异常。

一、getLocalHost方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package cn.k88.net_Demo;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 使用getLocalHost可以得到描述本机IP的InetAddress对象
* public static InetAddress getLocalHost() throws UnknownHostException
* @author wangzheng
*
*/

public class InetAddress_getLocalHost {

public static void main(String[] args) throws UnknownHostException {
InetAddress localaddress=InetAddress.getLocalHost(); //static method.
System.out.println(localaddress); // (hostname/ip address)
try {
System.out.println(localaddress.isReachable(5));

//Test whether that address is reachable
} catch (IOException e) {

e.printStackTrace();
}

}

}

运行结果:

k88/192.168.191.5
false

二、getByName方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.k88.net_Demo;

import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 这个方法是InetAddress类最常用的方法。它可以通过<strong>指定域名</strong>从DNS中得到<strong>相应的IP地址</strong>。
* getByName一个String类型参数,可以通过这个参数指定远程主机的域名,它的定义如下:
* public static InetAddress getByName(String host) throws UnknownHostException
* @author wangzheng
*
*/

public class InetAddress_getByName {

public static void main(String[] args) throws UnknownHostException {
String host="baidu.com";
InetAddress address=InetAddress.getByName(host);
System.out.println(address);

}

}

运行结果:

baidu.com/220.181.57.217

三、getAllByName方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.k88.net_Demo;

import java.net.InetAddress;
import java.net.UnknownHostException;
/**使用getAllByName方法可以从DNS上得到<strong>域名对应的所有的IP</strong>。
* 这个方法返回一个InetAddress类型的数组。这个方法的定义如下:
* public static InetAddress[] getAllByName(String host)
* throws UnknownHostException
* @author wangzheng
*
*/

public class InetAddress_getAllByName {

public static void main(String[] args) throws UnknownHostException {
String host="baidu.com";
InetAddress []address=InetAddress.getAllByName(host);
for(InetAddress net:address)
{
System.out.println(net);
}

}

}

运行结果:

baidu.com/220.181.57.217
baidu.com/180.149.132.47
baidu.com/123.125.114.144

四、getByAddress方法

这个方法必须通过IP地址来创建InetAddress对象,而且IP地址必须是byte数组形式。getByAddress方法有两个重载形式,定义如下:

  • public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
  • public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package cn.k88.net_Demo;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddress_getByAddress {

public static void main(String[] args) throws UnknownHostException {
byte[] addr=new byte[]{(byte)220,(byte)181,(byte)57,(byte)217};
InetAddress host=InetAddress.getByAddress(addr);
System.out.println(host.getHostName());

}

}

运行结果

220.181.57.217


java.net.InetAddress类