如何解决使用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();
}
}
解决方法
正在发生SocketException
。您不打印它或对其执行任何操作,因此此后没有任何反应,也看不到它。如果您执行exp.getCause()
之类的操作而不是exp.printStackTrace()
(不执行任何操作),则会看到问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。