Java 实例 - 获取指定主机的IP地址

3441 1
LZH实名认证 2022-4-9 18:28:06 | 显示全部楼层 |阅读模式
以下实例演示了如何使用 InetAddress 类的 InetAddress.getByName() 方法来获取指定主机(网址)的IP地址:

Main.java 文件
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIP {
    public static void main(String[] args) {
        InetAddress address = null;
        try {
            address = InetAddress.getByName("www.runoob.com");
        }
        catch (UnknownHostException e) {
            System.exit(2);
        }
        System.out.println(address.getHostName() + "=" + address.getHostAddress());
        System.exit(0);
    }
}
以上代码运行输出结果为:

www.runoob.com=222.73.134.120
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行