关闭

Java网络编程之查找Internet地址

发表于:2015-8-06 09:15

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:温布利往事    来源:51Testing软件测试网采编

  一、概述
  连接到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 InetAddressgetByName(String host)
  Determines the IP address of a host, given the host's name.
  static InetAddressgetLocalHost()
  Returns the address of the local host.
  ps:需要只指出的是,这些方法不只是使用它们的参数来设置内部字段,还需要进行网络连接来获取所需的所有信息。这个类的其他方法则主要使用上述方法提供的信息来工作
  由于DNS查找成本相对较高,InetAddress类缓存查找的结果,可以通过networkaddress.cache.ttl指定成功的DNS查找在Java缓存中保留的秒数。除了在InetAddress类中的本地化缓存,本地主机、本地域名服务器和Internet中其他地方的DNS服务器也会缓存各种查找结果。
  2、类方法
booleanequals(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 InetAddressgetByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address .
static InetAddressgetByAddress(String host, byte[] addr)
Creates an InetAddress based on the provided host name and IP address.
static InetAddressgetByName(String host)
Determines the IP address of a host, given the host's name.
StringgetCanonicalHostName()
Gets the fully qualified domain name for this IP address.
StringgetHostAddress()
Returns the IP address string in textual presentation.
StringgetHostName()
Gets the host name for this IP address.
static InetAddressgetLocalHost()
Returns the address of the local host.
static InetAddressgetLoopbackAddress()
Returns the loopback address.
inthashCode()
Returns a hashcode for this IP address.
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号