InetAddress的几个常用的static方法

来源:互联网 发布:易建联体测数据 编辑:程序博客网 时间:2024/05/14 17:36

1. getAllByName(String host)

给定主机名的所有 IP 地址所组成的数组。


2. getByName(String host)

给定主机名的 IP 地址。


3. getLocalHost()

本地主机的 IP 地址。


4.getLoopbackAddress()

返回回送地址


下面给出测试程序:

package com.niepengfei.test;import java.net.InetAddress;public class TestInetAddress {public static void main(String[] args) throws Exception {InetAddress[] inetAddressArray = InetAddress.getAllByName("baidu.com");for(int i=0;i<inetAddressArray.length;i++){InetAddress addr = inetAddressArray[i];System.out.println(addr.getHostName()+" "+addr.getHostAddress());}System.out.println("-----------------------------------------------");InetAddress address = InetAddress.getByName("baidu.com");System.out.println(address.getHostName()+" "+address.getHostAddress());System.out.println("-----------------------------------------------");InetAddress locaAddress = InetAddress.getLocalHost();System.out.println(locaAddress.getHostName()+" "+locaAddress.getHostAddress());System.out.println("-----------------------------------------------");InetAddress loopbackAddress = InetAddress.getLoopbackAddress();System.out.println(loopbackAddress.getHostName()+" "+loopbackAddress.getHostAddress());}}

测试结果如下:






0 0
原创粉丝点击