Android(java)學習筆記79:java中InetAddress類概述和使用

來源:互聯網
上載者:User

標籤:

要想讓網路中的電腦能夠互相通訊,必須為每台電腦指定一個標識號,通過這個標識號來指定要接受資料的電腦和識別發送的電腦。

在TCP/IP協議中,這個標識號就是IP地址。

那麼,我們如果擷取和操作IP地址呢? 為了方便我們對IP地址的擷取和操作,java提供了一個類InetAddress 供我們使用。

 

 1 package cn.itcast_01; 2  3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5  6 /* 7  * 如果一個類沒有構造方法: 8  * A:成員全部是靜態(Math,Arrays,Collections) 9  * B:單例設計模式(Runtime)10  * C:類中有靜態方法返回該類的對象(InetAddress)11  *         class Demo {12  *             private Demo(){}13  * 14  *             public static Demo getXxx() {15  *                 return new Demo();16  *             }17  *         }18  * 19  * 看InetAddress的成員方法:20  * public static InetAddress getByName(String host):根據主機名稱或者IP地址的字串表示得到IP地址對象21  */22 public class InetAddressDemo {23     public static void main(String[] args) throws UnknownHostException {24         // public static InetAddress getByName(String host)25         // InetAddress address = InetAddress.getByName("liuyi");26         // InetAddress address = InetAddress.getByName("192.168.12.92");27         InetAddress address = InetAddress.getByName("192.168.12.63");28 29         // 擷取兩個東西:主機名稱,IP地址30         // public String getHostName()31         String name = address.getHostName();32         // public String getHostAddress()33         String ip = address.getHostAddress();34         System.out.println(name + "---" + ip);35     }36 }

 

Android(java)學習筆記79:java中InetAddress類概述和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.