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: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 东台市| 方正县| 江西省| 黄石市| 富锦市| 丘北县| 香河县| 鄄城县| 西充县| 新和县| SHOW| 平山县| 溧水县| 通化市| 广灵县| 景宁| 洛川县| 光山县| 秭归县| 宁安市| 原平市| 弥勒县| 灌云县| 淳化县| 惠安县| 吴忠市| 凤台县| 横山县| 肇东市| 襄樊市| 巩留县| 扶余县| 高青县| 黔西县| 兴城市| 金山区| 柳河县| 准格尔旗| 密山市| 辽宁省| 高密市|