Java网络编程之查找Internet地址(2)

Creates an InetAddress based on the provided host name and IP address.

 
static InetAddress   (String host)

Determines the IP address of a host, given the host's name.

 
String   ()

Gets the fully qualified domain name for this IP address.

 
String   ()

Returns the IP address string in textual presentation.

 
String   ()

Gets the host name for this IP address.

 
static InetAddress   ()

Returns the address of the local host.

 
static InetAddress   ()

Returns the loopback address.

 
int   ()

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();
        }
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ee01aa0ce7161cd9918ab5d7064685b3.html