2010-05-16 105 views
7

如何獲得使用Java的計算機的局域網IP地址?我需要連接到路由器和網絡其餘部分的IP地址。如何獲取使用Java的客戶端的LAN IP?

我已經試過這樣的事情:

Socket s = new Socket("www.google.com", 80); 
String ip = s.getLocalAddress().getHostAddress(); 
s.close(); 

這似乎在某些情況下工作,但有時它返回回送地址或完全不同的東西。此外,它需要互聯網連接。

有沒有人有更準確的方法做到這一點?

編輯:認爲這將是更好的要求比在這裏評論..

,如果你有什麼很多的接口?例如,一個用於電纜,一個用於wifi,一個用於虛擬盒子等。是不可能真正看到哪一個連接到網絡?

回答

15

嘗試java.net.NetworkInterface

import java.net.NetworkInterface; 

... 

for (
    final Enumeration<NetworkInterface> interfaces = 
     NetworkInterface.getNetworkInterfaces(); 
    interfaces.hasMoreElements(); 
) 
{ 
    final NetworkInterface cur = interfaces.nextElement(); 

    if (cur.isLoopback()) 
    { 
     continue; 
    } 

    System.out.println("interface " + cur.getName()); 

    for (final InterfaceAddress addr : cur.getInterfaceAddresses()) 
    { 
     final InetAddress inet_addr = addr.getAddress(); 

     if (!(inet_addr instanceof Inet4Address)) 
     { 
      continue; 
     } 

     System.out.println(
      " address: " + inet_addr.getHostAddress() + 
      "/" + addr.getNetworkPrefixLength() 
     ); 

     System.out.println(
      " broadcast address: " + 
       addr.getBroadcast().getHostAddress() 
     ); 
    } 
} 
+3

+1 for'if(cur.isLoopback())' – aioobe 2010-05-16 20:20:21

+0

如果你有很多接口呢?例如,一個用於電纜,一個用於wifi,一個用於虛擬盒子等等。是不可能實際看到哪一個連接到網絡? – cragiz 2010-05-16 21:05:23

+0

@亨利克。在這種情況下,所有這些都是「連接」的。由您的操作系統決定哪一個將用於外部路由。你可以使用這個Socket構造函數http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket(java.net.InetAddress,%20int,%20java.net。 InetAddress,%20int)來選擇特定的本地地址和端口,或者構建默認套接字並將其綁定到特定接口。另外,請檢查您是否可以從NetworkInterface對象確定需要的信息。 – 2010-05-17 11:56:03

7

起初:沒有一個地址。您的機器至少有兩個地址(「lo」上爲127.0.0.1,「eth1」上爲192.168.1.1)。

你想這樣的:Listing network interfaces

正如你所期待,你不能自動檢測出連接到任何你的路由器的,因爲這需要你的路由表的潛在複雜的分析。但如果你只是想要任何非本地地址,這應該足夠了。可以肯定的是,至少在Vista或Windows 7上至少使用一次,因爲它們會添加IPv6地址。

import java.io.*; 
import java.net.*; 
import java.util.*; 
import static java.lang.System.out; 

public class ListNets 
{ 
    public static void main(String args[]) throws SocketException { 
     Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); 
     for (NetworkInterface netint : Collections.list(nets)) 
      displayInterfaceInformation(netint); 
    } 

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { 
     out.printf("Display name: %s\n", netint.getDisplayName()); 
     out.printf("Name: %s\n", netint.getName()); 
     Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); 
     for (InetAddress inetAddress : Collections.list(inetAddresses)) { 
      out.printf("InetAddress: %s\n", inetAddress); 
     } 
     out.printf("\n"); 
    } 
} 

以下是從例如程序輸出樣本:

Display name: bge0 
Name: bge0 
InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2 
InetAddress: /121.153.225.59 

Display name: lo0 
Name: lo0 
InetAddress: /0:0:0:0:0:0:0:1%1 
InetAddress: /127.0.0.1 
0
try { 
    InetAddress addr = InetAddress.getLocalHost(); 

    // Get IP Address 
    byte[] ipAddr = addr.getAddress(); 

    // Get hostname 
    String hostname = addr.getHostName(); 
} catch (UnknownHostException e) { 
} 
1

這是我用於方法而。它還包括一些小技巧來找出外部可見的IP地址。

private List<String> getLocalHostAddresses() { 

    List<String> addresses = new ArrayList<String>(); 

    try { 
     Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); 

     while (e.hasMoreElements()) { 
      NetworkInterface ni = e.nextElement(); 
      Enumeration<InetAddress> e2 = ni.getInetAddresses(); 
      while (e2.hasMoreElements()) 
       addresses.add(e2.nextElement().getHostAddress()); 
     } 
     URL u = new URL("http://whatismyip.org"); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       u.openStream())); 
     addresses.add(in.readLine()); 
     in.close(); 
    } catch (Exception ignore) { 
    } 

    return addresses; 
} 
0

正如Daniel已經指出的那樣,您無法知道哪個接口是「連接」的接口。例如,如果計算機有多個網絡接口卡都連接到單獨的物理局域網?

讓用戶決定使用哪個接口或全部嘗試它們,具體取決於您的用例。