[英]Issue With Getting Every IP Address of the Client in Java
I am having an issue with getting every IP address in Java. 我在使用Java获取每个IP地址时遇到问题。 When I open the GUI to select which IP you want to use, I call:
当我打开GUI以选择您要使用的IP时,我打电话给:
private List<String> getIP() {
List<String> outputList = new ArrayList<String>();
try {
InetAddress localIP = InetAddress.getLocalHost();
InetAddress[] everyIPAddress = InetAddress.getAllByName(localIP
.getCanonicalHostName());
if (everyIPAddress != null && everyIPAddress.length > 1) {
for (int i = 0; i < everyIPAddress.length; i++) {
if (!everyIPAddress[i].toString().contains(":")) {
outputList.add(everyIPAddress[i].toString());
}
}
}
} catch (UnknownHostException e) {
System.out.println("Error finding IP Address");
}
return outputList;
}
This method gets all of the IPv4 addresses that the client has. 此方法获取客户端具有的所有IPv4地址。 I know IPv6 addresses contain colons, so I don't add any with a colon to the list.
我知道IPv6地址包含冒号,因此我不会在列表中添加冒号。
Then, pressing the button changes the IP address. 然后,按下按钮可更改IP地址。 However, I have noticed that when there is only one IPv4 address that the machine has (You get two from having a service like Hamachi) it will return a null exception.
但是,我注意到当机器只有一个IPv4地址时(你得到两个像Hamachi这样的服务),它将返回一个空例外。 How would I go about getting every IP address of the client without returning a null exception if there is only one address?
如果只有一个地址,如何在不返回空例外的情况下获取客户端的每个IP地址?
if (everyIPAddress != null && everyIPAddress.length > 1) {
should be 应该
if (everyIPAddress != null && everyIPAddress.length >= 1) {
or 要么
if (everyIPAddress != null && everyIPAddress.length > 0) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.