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

          導航

          常用鏈接

          留言簿(8)

          隨筆分類(45)

          隨筆檔案(82)

          文章檔案(2)

          Java Spaces

          搜索

          •  

          積分與排名

          • 積分 - 66086
          • 排名 - 814

          最新評論

          閱讀排行榜

          評論排行榜

           

          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 閱讀(563) 評論(0)  編輯  收藏 所屬分類: Java Memo
           
          Copyright © Dedian Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 宜良县| 河津市| 庆阳市| 常宁市| 修武县| 溧水县| 罗江县| 曲沃县| 裕民县| 锡林郭勒盟| 彭州市| 普兰店市| 宜都市| 且末县| 阳高县| 慈溪市| 江门市| 五华县| 金平| 清水县| 宁远县| 盘锦市| 长治县| 台中县| 西乡县| 大竹县| 抚松县| 宜兴市| 永吉县| 平顶山市| 兴化市| 榆中县| 芦溪县| 类乌齐县| 德化县| 遵义市| 通道| 和林格尔县| 铁岭市| 大洼县| 沁水县|