Java 獲取本機的ip和主機名

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Util{

  /**
   * Finds a local, non-loopback, IPv4 address
   * 
   * @return The first non-loopback IPv4 address found, or
   *         <code>null</code> if no such addresses found
   * @throws SocketException
   *            If there was a problem querying the network
   *            interfaces
   */
  public static InetAddress getLocalAddress() throws SocketException
  {
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while( ifaces.hasMoreElements() )
    {
      NetworkInterface iface = ifaces.nextElement();
      Enumeration<InetAddress> addresses = iface.getInetAddresses();

      while( addresses.hasMoreElements() )
      {
        InetAddress addr = addresses.nextElement();
        if( addr instanceof Inet4Address && !addr.isLoopbackAddress() )
        {
          return addr;
        }
      }
    }

    return null;
  }

}


一、相關loopback address基本概念      

 

      路由器上設備的Loopback地址是什麼意思?

 

      本地環回接口(或地址),亦稱回送地址(loopback address)。

    

      此類接口是應用最爲廣泛的一種虛接口,幾乎在每臺路由器上都會使用。常見於如下用途: 
1 作爲一臺路由器的管理地址 
系統管理員完成網絡規劃之後,爲了方便管理,會爲每一臺路由器創建一個loopback 接口,並在該接口上單獨指定一個IP 地址作爲管理地址,管理員會使用該地址對路由器遠程登錄(telnet ),該地址實際上起到了類似設備名稱一類的功能。 
但是通常每臺路由器上存在衆多接口和地址,爲何不從當中隨便挑選一個呢? 
原因如下:由於telnet 命令使用TCP 報文,會存在如下情況:路由器的某一個接口由於故障down 掉了,但是其他的接口卻仍舊可以telnet ,也就是說,到達這臺路由器的TCP 連接依舊存在。所以選擇的telnet 地址必須是永遠也不會down 掉的,而虛接口恰好滿足此類要求。由於此類接口沒有與對端互聯互通的需求,所以爲了節約地址資源,loopback 接口的地址通常指定爲32 位掩碼。 
2 使用該接口地址作爲動態路由協議OSPF 、BGP 的router id 動態路由協議OSPF 、BGP 在運行過程中需要爲該協議指定一個Router id ,作爲此路由器的唯一標識,並要求在整個自治系統內唯一。由於router id 是一個32 位的無符號整數,這一點與IP 地址十分相像。而且IP 地址是不會出現重複現象的,所以通常將路由器的router id 指定爲與該設備上的某個接口的地址相同。由於loopback 接口的IP 地址通常被視爲路由器的標識,所以也就成了router id 的最佳選擇。 
3、使用該接口地址作爲BGP 建立TCP 連接的源地址 
在BGP 協議中,兩個運行BGP 的路由器之間建立鄰居關係是通過TCP 建立連接完成的。 
在配置鄰居時通常指定loopback 接口爲建立TCP 連接的源地址(通常只用於IBGP ,原因同2.1 ,都是爲了增強TCP 連接的健壯性) 
配置命令如下: 
router id 61.235.66.1 
interface loopback 0 
ip address 61.235.66.1 255.255.255.255 
router bgp 100 
neighbor 61.235.66.7 remote-as 200 
neighbor 61.235.66.7 update-source LoopBack0 
4、在Windows系統中,採用127.0.0.1作爲本地環回地址。 

 

二、Java類中運用

public class InetAddress
extends Objectimplements Serializable

此類表示互聯網協議 (IP) 地址。

 

InetAddress 的實例包含 IP 地址,還可能包含相應的主機名(取決於它是否用主機名構造或者是否已執行反向主機名解析)。 

 boolean isLoopbackAddress() 
          檢查 InetAddress 是否是回送地址的實用例行程序。

 

 

 

 

 

獲得了InetAddress子類對象的引用就可以調用InetAddress的各種方法來獲得InetAddress子類對象中的IP地址信息,比如,可以通過調用getCanonicalHostName()從域名服務中獲得標準的主機名;getHostAddress()獲得IP地址,getHostName()獲得主機名,isLoopbackAddress()判斷IP地址是否是一個loopback地址。

List1 是一段示範代碼:

 

// InetAddressDemo.java
import java.net.*;

class InetAddressDemo
{
 public static void main (String [] args) throws UnknownHostException
 {
  String host = "localhost";

  if (args.length == 1)
   host = args [0];

  InetAddress ia = InetAddress.getByName (host);

  System.out.println ("Canonical Host Name = " +
        ia.getCanonicalHostName ());
  System.out.println ("Host Address = " +
        ia.getHostAddress ());
  System.out.println ("Host Name = " +
        ia.getHostName ());
  System.out.println ("Is Loopback Address = " +
        ia.isLoopbackAddress ());
 }
}

當無命令行參數時,代碼輸出類似下面的結果:

 

Canonical Host Name = localhost

Host Address = 127.0.0.1

Host Name = localhost

Is Loopback Address = true



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章