其他分享
首页 > 其他分享> > InetAddress对象的使用

InetAddress对象的使用

作者:互联网

package demo03;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class API {
    public static void main(String[] args) throws UnknownHostException {
//        获取本机inetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);

//        根据主机名获取inetAddress对象
        InetAddress host1 = InetAddress.getByName("ZX");
        System.out.println(host1);

//        根据域名返回inetAddress对象
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println(host2);

//        通过InetAddress对象,获取对应的地址
        String hostAddress = host2.getHostAddress();
        System.out.println("host2对应的IP:"+hostAddress);

//        通过InetAddress对象,获取域名或者主机名
        String name1 = host2.getHostName();
        String name2 = host1.getHostName();
        System.out.println(name1+"\n"+name2);
    }
}

标签:String,对象,System,host2,使用,println,InetAddress,out
来源: https://blog.csdn.net/m0_52170039/article/details/122354286