14

您如何确定使用 Java 连接到 Internet 的网络接口?例如,我跑

InetAddress.getLocalHost().getHostAddress();

在 Eclipse 中,这正是我想要的,192.168.1.105。但是,如果我把它打包成一个 jar 文件并运行程序,代码会返回 169.254.234.50。对此进行调查,我发现这是我机器上 VMware 虚拟以太网适配器接口的 IP 地址。

有什么方法可以确定连接到互联网的接口,同时保持我的代码的可移植性?

接口比较

接口 [net4]

display name  : Intel(R) Centrino(R) Ultimate-N 6300 AGN
MTU           : 1500
loopback      : false
point to point: false
up            : true
virtual       : false
multicast     : true
HW address    : 00 24 D7 2C 5F 70 
INET address (IPv4): 192.168.1.105
  host name           : MyComputer
  canonical host name : MyComputer
  loopback            : false
  site local          : true
  any local           : false
  link local          : false
  multicast           : false
  reachable           : true

接口 [eth5]

display name  : VMware Virtual Ethernet Adapter for VMnet1
MTU           : 1500
loopback      : false
point to point: false
up            : true
virtual       : false
multicast     : true
HW address    : 00 50 56 C0 00 01 
INET address (IPv4): 169.254.234.50
  host name           : MyComputer
  canonical host name : MyComputer
  loopback            : false
  site local          : false
  any local           : false
  link local          : true
  multicast           : false
  reachable           : true

还有第三个 VMware 界面,站点 local=true 和链接 local=false,因此这些字段也没有任何帮助。

4

4 回答 4

22

在我的笔记本电脑(运行 Windows 7,安装了 Virtual Box 和它的网络接口)上,以下代码打印出我的无线接口的名称以及我的本地地址。它在一天结束时使用蛮力方法,但只会尝试并实际连接到被认为是最佳候选者的地址。

// iterate over the network interfaces known to java
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
OUTER : for (NetworkInterface interface_ : Collections.list(interfaces)) {
  // we shouldn't care about loopback addresses
  if (interface_.isLoopback())
    continue;

  // if you don't expect the interface to be up you can skip this
  // though it would question the usability of the rest of the code
  if (!interface_.isUp())
    continue;

  // iterate over the addresses associated with the interface
  Enumeration<InetAddress> addresses = interface_.getInetAddresses();
  for (InetAddress address : Collections.list(addresses)) {
    // look only for ipv4 addresses
    if (address instanceof Inet6Address)
      continue;

    // use a timeout big enough for your needs
    if (!address.isReachable(3000))
      continue;

    // java 7's try-with-resources statement, so that
    // we close the socket immediately after use
    try (SocketChannel socket = SocketChannel.open()) {
      // again, use a big enough timeout
      socket.socket().setSoTimeout(3000);

      // bind the socket to your local interface
      socket.bind(new InetSocketAddress(address, 8080));

      // try to connect to *somewhere*
      socket.connect(new InetSocketAddress("google.com", 80));
    } catch (IOException ex) {
      ex.printStackTrace();
      continue;
    }

    System.out.format("ni: %s, ia: %s\n", interface_, address);

    // stops at the first *working* solution
    break OUTER;
  }
}

(我已经isReachable(...)根据Mocker Tim 的回答更新了我的回答。)

需要注意的一件事。socket.bind(...)如果我试图连续太快地运行我的代码,比如连接没有足够快地清理,我会咆哮地址和端口已经在使用中。8080也许应该是一个随机端口。

于 2011-12-11T07:26:22.387 回答
5

查看Java 教程中的检索网络接口列出网络接口地址

如果您有多个已启动并正在运行的网络接口,则必须以编程方式选择程序应使用的网络接口。

更新:

我找到了和你类似的问题。请参阅how-to-check-if-internet-connection-is-present-in-java
的答案。

更新 2:

恕我直言,您应该在程序中执行以下操作:

  1. 查找运行程序的机器上的所有 IPv4 网络接口;
  2. 询问用户你的程序应该使用哪一个;
  3. 使用用户指向的界面。
于 2011-12-11T07:15:53.013 回答
3

我遇到了同样的问题,并提出了这个解决方案(不像答案那么彻底,但我需要一些没有尝试连接到互联网的东西。

基于 Mac 地址的已知 OUI:http: //standards-oui.ieee.org/oui.txt

我做了一个快速检查(可以用二进制逻辑优化)

private static boolean isVmwareMac(byte[] mac) {
    byte invalidMacs[][] = {
            {0x00, 0x05, 0x69},             //VMWare
            {0x00, 0x1C, 0x14},             //VMWare
            {0x00, 0x0C, 0x29},             //VMWare
            {0x00, 0x50, 0x56}              //VMWare
    };

    for (byte[] invalid: invalidMacs){
        if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2])
            return true;
    }

    return false;
}
于 2015-12-03T21:28:17.187 回答
1

我被同样的问题困扰了很久,尽管最初的问题是很久以前提出的,但无论如何我都会附加我对这个问题的看法。具有讽刺意味的是,答案是与问题一起提供的。
它是:siteLocal = true
这对我有用

  1. 获取所有网络接口

    NetworkInterface.getNetworkInterfaces();
    
  2. 挑出那些当前宕机的(只是减少运行次数)

    if(interface.isUp()){}
    
  3. 获取您刚刚挑选出来的接口的 InetAddresses

    interface.getInetAddresses();
    
  4. 检查这些地址是否是站点本地地址

    if(inetAddress.isSiteLocal()){}
    

这应该为您提供一个/所有 InetAddress(es) - 以及链接到您的本地网络的网络接口。
本地站点被定义为使用来自这些网络的地址:10.0.0.0、172.16.0.0、192.168.0.0,而 VirtualBox 适配器和其他通常使用 169.254.0.0 链接本地地址。IPv6 也实现了站点本地的概念,所以这里最好重点关注 IPv4。在普通的最终用户机器上,这应该工作得很好,并且只返回一个物理接口。

于 2018-05-11T11:29:13.120 回答