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

          導航

          常用鏈接

          留言簿(8)

          隨筆分類(45)

          隨筆檔案(82)

          文章檔案(2)

          Java Spaces

          搜索

          •  

          積分與排名

          • 積分 - 65551
          • 排名 - 817

          最新評論

          閱讀排行榜

          評論排行榜

           

          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: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 长阳| 萝北县| 布尔津县| 于田县| 凤台县| 双流县| 平武县| 阿合奇县| 安康市| 岗巴县| 汝城县| 双鸭山市| 余姚市| 中超| 犍为县| 和林格尔县| 德惠市| 滦南县| 安化县| 辽阳市| 雷波县| 鸡泽县| 微山县| 越西县| 明星| 邓州市| 雷山县| 汉沽区| 嵊州市| 万源市| 崇州市| 晋宁县| 湄潭县| 荣昌县| 理塘县| 灌阳县| 崇州市| 洞口县| 浦城县| 东乡县| 望都县|