<>需求:获取本机主机地址,通过域名获取服务器主机地址,通过主机名获取主机地址
package cn.itsp; import java.net.Inet6Address; import java.net.InetAddress;
import java.net.UnknownHostException; public class InetAddressDemo { public
static void main(String[] args) throws UnknownHostException { InetAddress
localHost = Inet6Address.getLocalHost();
System.out.println(localHost);//LAPTOP-EQAGT6K3/192.168.200.1 String
hostAddress = localHost.getHostAddress();
System.out.println(hostAddress);//192.168.200.1
System.out.println(localHost.getHostName());//LAPTOP-EQAGT6K3 InetAddress
byName = Inet6Address.getByName("www.baidu.com");
System.out.println(byName);//www.baidu.com/163.177.151.109
System.out.println(byName.getHostAddress());//163.177.151.109
System.out.println(Inet6Address.getByName("LAPTOP-EQAGT6K3"));//LAPTOP-
EQAGT6K3/192.168.200.1
System.out.println(Inet6Address.getByName("LAPTOP-EQAGT6K3").getHostAddress());//192.168.200.1
} }

技术
今日推荐
PPT
阅读数 82
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信