Java网络编程从入门到精通(2):创建InetAddress对象的四个静态方法

来源:互联网 发布:尼康矩阵测光图标 编辑:程序博客网 时间:2024/03/28 22:19
   InetAddress类是Java中用于描述IP地址的类。它在java.net包中。在Java中分别用Inet4AddressInet6Address类来描述IPv4IPv6的地址。这两个类都是InetAddress的子类。由于InetAddress没有public的构造方法,因此,要想创建InetAddress对象,必须得依靠它的四个静态方法。InetAddress可以通过getLocalHost方法得到本机的InetAddress对象,也可以通过getByNamegetAllByNamegetByAddress得到远程主机的InetAddress对象。

一、getLocalHost方法

使用getLocalHost可以得到描述本机IPInetAddress对象。这个方法的定义如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public static InetAddress getLocalHost() throws UnknownHostException

这个方法抛出了一个UnknownHostException异常,因此,必须在调用这个方法的程序中捕捉或抛出这个异常。下面的代码演示了如何使用getLocalHost来得到本机的IP和计算机名。

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->package inet;

import java.net.*;

public class MyInetAddress1
{
    
public static void main(String[] args) throws Exception
    {
        InetAddress localAddress 
= InetAddress.getLocalHost();
        System.out.println(localAddress);        
    }
}

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->ComputerName/192.168.18.10

InetAddress类中覆盖了Object类的toString方法,实现如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public String toString() 
{
     return ((hostName != null? hostName : ""+ "/" + getHostAddress();
}

从上面的代码可以看出,InetAddress方法中的toString方法返回了用“/“隔开的主机名和IP地址。因此,在上面的代码直接通过localAddress对象来输出本机计算机名和IP地址(将对象参数传入println方法后,println方法会调用对象参数的toString方法来输出结果)。

当本机绑定了多个IP时,getLocalHost只返回第一个IP。如果想返回本机全部的IP,可以使用getAllByName方法。

二、getByName方法

这个方法是InetAddress类最常用的方法。它可以通过指定域名从DNS中得到相应的IP地址。getByName一个String类型参数,可以通过这个参数指定远程主机的域名,它的定义如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public static InetAddress getByName(String host) throws UnknownHostException

如果host所指的域名对应多个IPgetByName返回第一个IP。如果本机名已知,可以使用getByName方法来代替getLocalHost。当host的值是localhost时,返回的IP一般是127.0.0.1。如果host是不存在的域名,getByName将抛出UnknownHostException异常,如果hostIP地址,无论这个IP地址是否存在,getByName方法都会返回这个IP地址(因此getByName并不验证IP地址的正确性)。下面代码演示了如何使用getByName方法:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  package inet;
  
  
import java.net.*;
  
  
public classMyInetAddress2
  {
      
public static void main(String[] args) throws Exception
      {
          
if (args.length == 0)
              
return;
          String host 
= args[0];
          InetAddress address 
= InetAddress.getByName(host);
          System.out.println(address);
      }
  }

  •  测试1:远程主机www.csdn.net

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress2 www.csdn.net

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->www.csdn.net/211.100.26.124
  • 测试2:本机名ComputerName

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress2 ComputerName

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->ComputerName/192.168.18.10
  • 测试3:代表本机的localhost

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress2 localhost

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->localhost/127.0.0.1

对于本机来说,除了可以使用本机名或localhost外,还可以在hosts文件中对本机做“IP/域名映射(在Windows操作系统下)。这个文件在C:\WINDOWS\system32\drivers\etc中。打开这两个文件中,在最后加一行如下所示的字符串:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->192.168.18.100   www.mysite.com

  • 测试4:本机域名www.mysite.com

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress2 www.mysite.com

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->www.mysite.com/192.168.18.100

getByName方法除了可以使用域名作为参数外,也可以直接使用IP地址作为参数。如果使用IP地址作为参数,输出InetAddress对象时域名为空(除非调用getHostName方法后,再输出InetAddress对象。getHostName方法将在下面的内容介绍)。读者可以使用129.42.58.212作为MyInetAddress2的命令行参数(这是www.ibm.comIP),看看会得到什么结果。

三、getAllByName方法

使用getAllByName方法可以从DNS上得到域名对应的所有的IP。这个方法返回一个InetAddress类型的数组。这个方法的定义如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->   public static InetAddress[] getAllByName(String host) throws UnknownHostException

   getByName方法一样,当host不存在时,getAllByName也会抛出UnknowHostException异常,getAllByName也不会验证IP地址是否存在。下面的代码演示了getAllByName的用法:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  package inet;
  
  
import java.net.*;
  
  
public classMyInetAddress3
  {
      
public static void main(String[] args) throws Exception
      {
          
if (args.length == 0)
              
return;
          String host 
= args[0];
          InetAddress addresses[] 
= InetAddress.getAllByName(host);
          
for (InetAddress address : addresses)
              System.out.println(address);
      }
  }

  • 测试1:远程主机www.csdn.net

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress3 www.csdn.net

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->www.csdn.net/211.100.26.124
www.csdn.net
/211.100.26.121
www.csdn.net
/211.100.26.122
www.csdn.net
/211.100.26.123

将上面的运行结果和例程3-2的测试1的运行结果进行比较,可以得出一个结论,getByName方法返回的IP地址就是getAllByName方法返回的第一个IP地址。事实上,getByName的确是这样实现的,getByName的实现代码如下:


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public static InetAddress getByName(String host) throws UnknownHostException
{
     return InetAddress.getAllByName(host)[0];
}
  •  测试2:使用www.csdn.net的一个IP 

执行如下命令:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->java inet.MyInetAddress3211.100.26.122

运行结果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/211.100.26.122

四、getByAddress方法

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

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException

第一个重载形式只需要传递byte数组形式的IP地址,getByAddress方法并不验证这个IP地址是否存在,只是简单地创建一个InetAddress对象。addr数组的长度必须是4IPv4)或16IPv6),如果是其他长度的byte数组,getByAddress将抛出一个UnknownHostException异常。第二个重载形式多了一个host,这个hostgetByNamegetAllByName方法中的host的意义不同,getByAddress方法并不使用hostDNS上查找IP地址,这个host只是一个用于表示addr的别名。下面的代码演示了getByAddress的两个重载形式的用法:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  packageinet;
  
  
import java.net.*;
  
  
public classMyInetAddress4
  {
      
public static void main(String[] args) throws Exception
      {
          
byte ip[] = new byte[] { (byte141, (byte1468 , 66};
          InetAddress address1 
= InetAddress.getByAddress(ip);
          InetAddress address2 
= InetAddress.getByAddress("Oracle官方网站", ip);
          System.out.println(address1);
          System.out.println(address2);
      }
  }

上面代码的运行结果如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/141.146.8.66
Oracle官方网站
/141.146.8.66

    从上面的运行结果可以看出,getByAddress只是简单地将host参数作为域名放到“/”前面,因此,host可以是任何字符串。

原创粉丝点击