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

          導航

          常用鏈接

          留言簿(8)

          隨筆分類(45)

          隨筆檔案(82)

          文章檔案(2)

          Java Spaces

          搜索

          •  

          積分與排名

          • 積分 - 66090
          • 排名 - 813

          最新評論

          閱讀排行榜

          評論排行榜

           

          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: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 广元市| 龙山县| 双峰县| 岳阳市| 砀山县| 盐亭县| 从化市| 黔江区| 潜江市| 肇源县| 南宫市| 岐山县| 如皋市| 亚东县| 商水县| 攀枝花市| 荆州市| 禄丰县| 兴海县| 山阴县| 溆浦县| 南岸区| 丽江市| 金秀| 寻甸| 青海省| 旬阳县| 永仁县| 乡城县| 岳阳市| 上栗县| 丰镇市| 达日县| 麻城市| 嘉荫县| 美姑县| 和顺县| 方城县| 九龙城区| 修文县| 霸州市|