Dedian  
          -- 關注搜索引擎的開發
          日歷
          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 82
          • 文章 - 2
          • 評論 - 228
          • 引用 - 0

          導航

          常用鏈接

          留言簿(8)

          隨筆分類(45)

          隨筆檔案(82)

          文章檔案(2)

          Java Spaces

          搜索

          •  

          積分與排名

          • 積分 - 65572
          • 排名 - 816

          最新評論

          閱讀排行榜

          評論排行榜

           

          1. Getting the IP Address of a Hostname

              try 
          {
          InetAddress addr = InetAddress.getByName("yahoo.com");
          byte[] ipAddr = addr.getAddress();

          // Convert to dot representation
          String ipAddrStr = "";
          for (int i=0; i<ipAddr.length; i++) {
          if (i > 0) {
          ipAddrStr += ".";
          }
          ipAddrStr += ipAddr[i]&0xFF;
          }
          }
          catch (UnknownHostException e) {
          }


          2. Getting the Hostname of an IP Address

          This example attempts to retrieve the hostname for an IP address. Note that getHostName() may not succeed, in which case it simply returns the IP address.

          try {
          // Get hostname by textual representation of IP address
          InetAddress addr = InetAddress.getByName("127.0.0.1");

          // Get hostname by a byte array containing the IP address
          byte[] ipAddr = new byte[]{127, 0, 0, 1};
          addr = InetAddress.getByAddress(ipAddr);

          // Get the host name
          String hostname = addr.getHostName();

          // Get canonical host name
          String hostnameCanonical = addr.getCanonicalHostName();
          } catch (UnknownHostException e) {
          }

          3. Getting the IP Address and Hostname of the Local Machine

              try {
          InetAddress addr = InetAddress.getLocalHost();

          // Get IP Address
          byte[] ipAddr = addr.getAddress();

          // Get hostname
          String hostname = addr.getHostName();
          } catch (UnknownHostException e) {
          }

          posted on 2006-08-18 06:53 Dedian 閱讀(559) 評論(0)  編輯  收藏 所屬分類: Java Memo
           
          Copyright © Dedian Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 瑞安市| 张家港市| 葫芦岛市| 华蓥市| 壶关县| 新余市| 上栗县| 津市市| 靖西县| 中山市| 荣昌县| 泰宁县| 彭阳县| 惠东县| 承德市| 古丈县| 外汇| 安阳市| 台东县| 宁化县| 永胜县| 根河市| 利川市| 卓尼县| 泾源县| 兴隆县| 陈巴尔虎旗| 丰县| 菏泽市| 炎陵县| 醴陵市| 辰溪县| 桂阳县| 汤阴县| 新昌县| 淮南市| 上饶县| 芮城县| 太白县| 通化县| 尼玛县|