[Java] InetAddress Demo (解释域名地址 获取本机ip) →→→→→进入此内容的聊天室

来自 , 2020-12-03, 写在 Java, 查看 148 次.
URL http://www.code666.cn/view/7bcdf75a
  1. import java.net.*;
  2.  
  3. /**
  4.  * InetAddress Demo
  5.  */
  6. public class InetAddressDemo {
  7.         public static void main(String[] args) {
  8.                 try {
  9.                         // 使用域名创建对象
  10.                         InetAddress inetAd = InetAddress.getByName("www.yuncode.net");// getByName是InetAddress类的静态方法
  11.                         System.out.println(inetAd);
  12.  
  13.                         // 使用IP创建对象
  14.                         InetAddress inetAd2 = InetAddress.getByName("127.0.0.1");
  15.                         System.out.println(inetAd2);
  16.  
  17.                         // 获得本机地址对象
  18.                         InetAddress inetAd3 = InetAddress.getLocalHost();
  19.                         System.out.println(inetAd3);
  20.  
  21.                         // 获得对象中储存的域名
  22.                         String host = inetAd3.getHostName();
  23.                         System.out.println("域名: " + host);
  24.  
  25.                         // 获得对象中储存的IP
  26.                         String ip = inetAd3.getHostAddress();
  27.                         System.out.println("IP: " + ip);
  28.                 } catch (Exception e) {
  29.                 }
  30.         }
  31. }

回复 "InetAddress Demo (解释域名地址 获取本机ip)"

这儿你可以回复上面这条便签

captcha