Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从网络接口获取Ip配置_Java_Networking_Network Programming_Network Interface - Fatal编程技术网

Java 如何从网络接口获取Ip配置

Java 如何从网络接口获取Ip配置,java,networking,network-programming,network-interface,Java,Networking,Network Programming,Network Interface,我有一个java应用程序,可以从网络接口中提取信息,我正在使用NetworkInterface类来迭代我计算机中的无数接口。“我已经安装了virtualbox”,这将创建许多虚拟界面。但我需要从我的无线或以太网接口获取信息,因为这些是连接到网络的唯一选项。问题是,我希望能够对所有这些接口进行排序,只获得相关的数据。你们有什么建议?在NetworkInteface集合中搜索eth0或wlan1。任何想法都值得赞赏。下面是在我的应用程序中处理网络接口的代码 //displaying all netw

我有一个java应用程序,可以从网络接口中提取信息,我正在使用NetworkInterface类来迭代我计算机中的无数接口。“我已经安装了virtualbox”,这将创建许多虚拟界面。但我需要从我的无线或以太网接口获取信息,因为这些是连接到网络的唯一选项。问题是,我希望能够对所有这些接口进行排序,只获得相关的数据。你们有什么建议?在NetworkInteface集合中搜索eth0或wlan1。任何想法都值得赞赏。下面是在我的应用程序中处理网络接口的代码

//displaying all network interface 
Enumeration<NetworkInterface> nets = null;
try {
   nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e3) {
    e3.printStackTrace();
}

for (NetworkInterface netint : Collections.list(nets)) {            
   displayInterfaceInformation(netint);
}

static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { 
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    System.out.printf(count + " Display name: %s\n", netint.getDisplayName());
    System.out.printf("Name: %s\n", netint.getName());
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {           
        System.out.printf("InetAddress: %s\n", inetAddress);
    }
    System.out.println("\n");
}
//显示所有网络接口
枚举网络=空;
试一试{
nets=NetworkInterface.getNetworkInterfaces();
}捕获(SocketException e3){
e3.printStackTrace();
}
对于(网络接口netint:Collections.list(nets)){
显示接口信息(netint);
}
静态无效显示接口信息(NetworkInterface netint)引发SocketException{
枚举inetAddresses=netint.getInetAddresses();
System.out.printf(计数+“显示名称:%s\n”,netint.getDisplayName());
System.out.printf(“名称:%s\n”,netint.getName());
对于(InetAddress:Collections.list(inetAddresses)){
System.out.printf(“InetAddress:%s\n”,InetAddress);
}
System.out.println(“\n”);
}

可能有一种更简单的方法,但如果你只是在寻找连接到internet的接口,有一个简单的技巧你可以使用,它不涉及读取路由表-只需创建一个套接字,将它连接到internet上的地址,然后获取该套接字的本地地址。最好使用UDP套接字,因为它的
connect
不会执行任何实际IO:

DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByName("1.1.1.1"), 53);
System.out.println(s.getLocalAddress());

操作系统将使用路由表将套接字绑定到出站连接的适当接口。一旦您有了IP地址,您就可以找到具有该IP地址的接口。

您是专门寻找eth0或wlan1,还是只寻找连接到internet的接口?无论是使用eth0还是wlan1进行连接,代码都应该工作。但是,是的,它只是为连接的接口显示信息。ie具有有效的ip地址,然后迭代每个网络接口以查找该本地ip地址?。。这可能是一个解决方案是的,这是一个想法。还没有实施它。。。但这是我将实施的解决方案之一