Java示例getHostAddress()和getHostName()


Java示例getHostAddress()和getHostName()

import java.net.InetAddress;

import java.net.UnknownHostException;



public class Demo {

   public static void main(String[] args) {

      InetAddress ipadd;

      String hostname;

      try {

         ipadd = InetAddress.getLocalHost();

         hostname = ipadd.getHostName();

         System.out.println("Your IP address : " + ipadd);

         System.out.println("Your Hostname : " + hostname);

      } catch (UnknownHostException e) {

      }

   }

}