风故故,也依依

Stand still in the wind.

导航

获取一个ip地址中的主机名

import java.net.*;

public class GetHostName {

     /**
      * @param args
      */
     public static void main(String[] args) {
         try{
             InetAddress addr = InetAddress.getByName("127.0.0.1");//在给定主机名的情况下确定主机的 IP 地址。
             byte[] ipAddr = new byte[]{127,0,0,1};
             addr = InetAddress.getByAddress(ipAddr);// 返回此 InetAddress 对象的原始 IP 地址。

             String hostName = addr.getHostName();//   获取此 IP 地址的主机名。
             System.out.println(hostName);

             String hostnameCanonical = addr.getCanonicalHostName();//获取此 IP 地址的完全限定域名。
             System.out.println(hostnameCanonical);
         }catch(Exception e){
             e.printStackTrace();
         }

     }

}

posted on 2007-06-18 02:31  jadmin  阅读(537)  评论(0编辑  收藏  举报