最起码这个方法逻辑有点问题
saddress = InetAddress.getByName(SIP);
SIPint = saddress.hashCode();
if (SIPint == 0)return false;
tempaddress = InetAddress.getLocalHost();看一下java.net.InetAddress.hashCode()的源代码,什么情况下散列值为0,是不是判断ip值是否为0.0.0.0。(上述分析供参考)
saddress = InetAddress.getByName(SIP);
SIPint = saddress.hashCode();
if (SIPint == 0)return false;
tempaddress = InetAddress.getLocalHost();看一下java.net.InetAddress.hashCode()的源代码,什么情况下散列值为0,是不是判断ip值是否为0.0.0.0。(上述分析供参考)
* Returns a hashcode for this IP address.
*
* @return a hash code value for this IP address.
*/
public int hashCode() {
return -1;
}
Object.hashCode()方法用来判断两个对象句柄是否引用同一个对象,InetAddress所有对象的hashCode()都相同,这样覆盖hashCode()失去了意义。
* Returns a hashcode for this IP address.
*
* @return a hash code value for this IP address.
*/
public int hashCode() {
return address;
}实际使用的是Inet4Address或Inet6Address的实例
public class Test {
public static void main(String args[]) throws Exception{
InetAddress add = InetAddress.getByName("localhost");
System.out.println(add);
System.out.println(add.hashCode());//java.net.Inet4Address.hashCode();
System.out.println(addr2int(new byte[]{127,0,0,1}));
System.out.println(addr2int(new byte[]{0,0,0,0})); //if (SIPint == 0)
}
static int addr2int(byte[] addr){
int address = addr[3] & 0xFF;
address |= ((addr[2] << 8) & 0xFF00);
address |= ((addr[1] << 16) & 0xFF0000);
address |= ((addr[0] << 24) & 0xFF000000);
return address;
}
}
SIPint = saddress.hashCode();
tempaddress = InetAddress.getLocalHost();
if (SIPint == 0)
return false;
上面那样也只是判断IP地址是否为0.0.0.0
而实际上地址配置为0.0.0.0
通过机器名得到的IP也应该会是127.0.0.1
import java.net.*;
public class Test {
public static void main(String args[]) throws Exception{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //慢慢地
InetAddress add;
for(int i=-128;i<=127;i++){//穷举所有的IPV4地址
for(int j=-128;j<=127;j++){
for(int k=-128;k<=127;k++){
for(int l=-128;l<=127;l++){
add = InetAddress.getByAddress(new byte[]{(byte)i,(byte)j,(byte)k,(byte)l,});
if(add.hashCode()==0){
System.out.println(i+","+j+","+k+","+l);
}
}
}
}
System.out.println("running...");
}
}
}
结果应该是IPV4为0.0.0.0时满足条件(可能要半小时左右)。关于0.0.0.0
严格说来,0.0.0.0已经不是一个真正意义上的IP地址了。它表示的是这样一个集合:所有不清楚的主机和目的网络。这里的“不清楚”是指在本机的路由表里没有特定条目指明如何到达。对本机来说,它就是一个“收容所”,所有不认识的“三无”人员,一律送进去。如果你在网络设置中设置了缺省网关,那么Windows系统会自动产生一个目的地址为0.0.0.0的缺省路由。我已尽力,如果没帮上忙,算我班门弄斧。