InetAddress(IP类)

来源:互联网 发布:mac vmware 安装win10 编辑:程序博客网 时间:2024/06/04 22:47

getLocalHost(); 获取本机的IP地址
getByName(String host) 在给定主机名的情况下生成IP 地址对象(用于获取别人的IP地址)

getAllByName:

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

在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。

getHostAddress() 返回一个IP地址的字符串表示形式
getHostName() 返回计算机的主机名

package com.network.test;import java.net.InetAddress;import java.net.UnknownHostException;public class Demo1 {    public static void main(String[] args) throws UnknownHostException{        /*        //getLocalHost 获取本机的IP地址对象        InetAddress address=InetAddress.getLocalHost();        System.out.println("Ip地址:"+address.getHostAddress());        System.out.println("主机名:"+address.getHostName());        */        //获取别人机器的IP地址对象        //可以根据一个IP地址的字符串形式或者主机名生成一个IP地址对象        InetAddress address=InetAddress.getByName("jun-PC");        System.out.println("IP地址:"+address.getHostAddress());        System.out.println("主机名:"+address.getHostName());        InetAddress[] arr=InetAddress.getAllByName("www.baidu.com");        for(InetAddress e:arr){            System.out.println(e);        }    }}
0 0