不说别的,直接上代码和运行结果图:
代码:
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getLocalHost();
System.err.print("主机名:");
System.out.println(inetAddress.getHostName());
System.err.println("本机的所有ip地址的列表:---");
Arrays.stream(InetAddress.getAllByName(inetAddress.getHostName())).forEach(i -> System.out.println(i.getHostAddress()));
}
运行结果:我现在连接的是WiFi,这个ip地址就是,但是没有获取到,不知道是否有大佬可以指点小弟一下
代码:
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getLocalHost();
System.err.print("主机名:");
System.out.println(inetAddress.getHostName());
System.err.println("本机的所有ip地址的列表:---");
Arrays.stream(InetAddress.getAllByName(inetAddress.getHostName())).forEach(i -> System.out.println(i.getHostAddress()));
}
运行结果:我现在连接的是WiFi,这个ip地址就是,但是没有获取到,不知道是否有大佬可以指点小弟一下
我改成了百度的dns之后可以正确获取到本机的所有网卡的ip
最后分享下公共的dns服务器,
114.114.114.114 不用说,国内通用
8.8.8.8 Google提供的,比较适合国外网站的浏览
180.76.76.76 百度提供的
223.5.5.5 223.6.6.6 阿里云提供的