Java网络编程之查找Internet地址

一、概述

  连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识。由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的IP地址联系在一起。通常一台主机映射一个IP地址。有时一个主机名映射多个IP。这时就由DNS负责随机选择一台机器来响应请求,例如业务流量很大的Web网站,它将负载分到多个系统上。

二、什么是IP地址?

  IP地址是IP使用的32为或128位无符号数字,它是一种低级协议,UDP和TCP协议都是在它的基础上构建的。

  1、地址类型

  • 单播:单个接口的标识符,发送到单播地址的数据包被交付给由该地址标示的接口。
  • 多播:一组接口(通常属于不同的节点)的标识符,发送到多播地址的数据包被交付给由该地址标示的所有接口。

  2、IP范围

  • 链接本地:地址设计用于在单个链接上寻址以解决自动地址配置、邻居发现或没有路由器时的问题。
  • 站点本地:地址设计用于在不需要全局前缀时站点内部寻址。
  • 全局地址:在Internet中是唯一的。

  3、IP地址的文本表示形式

  IP地址的文本表示形式是特定于地址系列的。

  IPV4地址一般写为四个无符号字节,每个字节范围从0到255。这种方式又称为点分四段格式。eg:192.168.1.119。

三、InetAddress类

  InetAddress类表示互联网协议(IP)地址,是Java对IP地址的高级表示。用于其他大多数网络类。

  InetAddress类的实例包含IP地址,还可能包含相应的主机名(取决于它是否用主机名构造或者是否已执行反向主机名解析)。

  1、构造InetAddress对象

  InetAddress类没有公共构造函数,可以通过其静态方法返回适当初始化的InetAddress对象。

static InetAddress[] getAllByName(String host)
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
static InetAddress getByName(String host)
Determines the IP address of a host, given the host's name.
static InetAddress getLocalHost()
    Returns the address of the local host.


ps:需要只指出的是,这些方法不只是使用它们的参数来设置内部字段,还需要进行网络连接来获取所需的所有信息。这个类的其他方法则主要使用上述方法提供的信息来工作。

  由于DNS查找成本相对较高,InetAddress类缓存查找的结果,可以通过networkaddress.cache.ttl指定成功的DNS查找在Java缓存中保留的秒数。除了在InetAddress类中的本地化缓存,本地主机、本地域名服务器和Internet中其他地方的DNS服务器也会缓存各种查找结果。

  2、类方法

boolean equals(Object obj)
Compares this object against the specified object.
byte[] getAddress()
Returns the raw IP address of this  InetAddress object.
static InetAddress[] getAllByName(String host)
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
static InetAddress getByAddress(byte[] addr)
Returns an  InetAddress object given the raw IP address .
static InetAddress getByAddress(String host, byte[] addr)
Creates an InetAddress based on the provided host name and IP address.
static InetAddress getByName(String host)
Determines the IP address of a host, given the host's name.
String getCanonicalHostName()
Gets the fully qualified domain name for this IP address.
String getHostAddress()
Returns the IP address string in textual presentation.
String getHostName()
Gets the host name for this IP address.
static InetAddress getLocalHost()
Returns the address of the local host.
static InetAddress getLoopbackAddress()
Returns the loopback address.
int hashCode()
Returns a hashcode for this IP address.

  3、InetAddress示例代码

public class Demo1
{

    public static void main(String[] args)
    {
        InetAddress ina;
        try
        {
            ina = InetAddress.getLocalHost();
            System.out.println(ina);
           
            System.out.println(ina.getAddress());//返回此 InetAddress 对象的原始 IP地址
           
            System.out.println(ina.getHostAddress());// 返回 IP 地址字符串(以文本表现形式)。
           
            System.out.println(ina.getHostName()); //获取此 IP 地址的主机名
           
            System.out.println(ina.getLocalHost()); //返回本地主机
        }
        catch (UnknownHostException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        InetAddress ina1;
        try
        {
            ina1 = InetAddress.getByName("192.168.1.119");
            System.out.println(ina1);
        }
        catch (UnknownHostException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        InetAddress[] ina2;
        try
        {
            ina2 = InetAddress.getAllByName("www.microsoft.com");
            for(int i=0;i<ina2.length;i++)
                System.out.println(ina2[i]);
        } catch (UnknownHostException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自www.linuxidc.com/Linux/2015-07/120216.htm