java - 使用 InetAddress 收集网络接口
问题描述
我使用 InetAddress 作为数组。但是,如果我不使用数组,我可以在循环中打印每个网络接口。虽然我正在迭代 InetAddress 数组,但代码没有显示任何内容。有根本问题吗?我对java中的网络世界很陌生。
import java.io.*;
import java.net.*;
import java.util.*;
public class Interface_Enumeration_Concept {
InetAddress[] address;
public void GetInterfaces(){
try {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) interfaces.nextElement();
Enumeration addresses = intf.getInetAddresses();
int i=0;
while (addresses.hasMoreElements()){
address[i] = (InetAddress) addresses.nextElement();
System.out.println(address[i]);
i++;
}
}
}catch(SocketException exp){
exp.getCause();
}
}
public static void main(String[] args) {
Interface_Enumeration_Concept objects = new Interface_Enumeration_Concept();
objects.GetInterfaces();
}
}
解决方案
ASocketException
正在发生。你不打印它,也不用它做任何事情,所以之后什么也没有发生,你也看不到它。如果不是exp.getCause()
(什么都不做)你做一些exp.printStackTrace()
你会看到问题的事情。
推荐阅读
- python - Telebot InlineKeyboard URL 无提示
- python - networkx 图的边缘标签不显示在 PyQt 嵌入式 matplotlib 中
- android - Android studio Artic fox 无法创建构建
- html - 使用 XSL 将 XML 转换为 HTMl 时丢失的值
- postgresql - 在需要比较运算符的 WHERE 子句中选择 count(*)
- security - 为什么 ModSecurity 会阻止外部链接?
- ruby - Selenium::WebDriver::Error::InvalidArgumentError:invalid 参数(会话信息:headless chrome=93.0.4577.82)
- ios - 虚幻引擎 4:如何在设备上设置虚拟键盘
- kubernetes - 如何在 prometheus 中获取最近 3 个月内运行的 pod
- java - javassist.CannotCompileException: [源错误] 找不到构造函数 org.apache.http.protocol.RequestUserAgent