获取另一个IP /站点的Inerface

时间:2016-08-27 13:16:02

标签: java networking network-programming

我正试图从任何网站或IP获取网络接口。例如,我想获得googl的开放网络接口。

从我的小代码示例中得到我的网络接口:

public class InterfaceLister {

    public static void main(String[] args) throws SocketException {

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            System.out.println(ni);
        }

    }
}

我如何像我一样(我的网络接口扫描代码),但对于另一个IP。如果有可能吗?

感谢

代码已更改:

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

public class InterfaceLister {

    public static void main(String[] args) throws SocketException, UnknownHostException {

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

        InetAddress google = InetAddress.getByName("www.google.com");
        NetworkInterface anotherInterface = NetworkInterface.getByInetAddress(google);

        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            System.out.println(ni);

            //if (anotherInterface != null)
            System.out.println("google: " + anotherInterface);
        }

    }
}

一些产出......

name:lo (Software Loopback Interface 1)
google: null
name:eth0 (Hyper-V Virtual Ethernet Adapter)
google: null
name:eth1 (Hyper-V Virtual Switch Extension Adapter)
google: null
name:net0 (Bluetooth Device (RFCOMM Protocol TDI))
google: null
name:wlan0 (Microsoft Hosted Network Virtual Adapter)
google: null

0 个答案:

没有答案