我正在尝试用Java编写一个程序来了解一台机器是否在全局/专用网络中。以下是我的代码段。我想如果机器只检测到环回地址(127.0.0.1
或::1
),那么可以假设机器不在网络中。但是在其中一个启用了多播的系统中,我正在获取除回送地址之外的IP地址。
fe80::20c:29ff:fe90:8041 ff01::1, ff02::1 ff02::1:ff90:8041 fe80::ffff:ffff:fffd ::1 fe80::1 127.0.0.1 224.0.0.1
代码段:
private static ArrayList< InetAddress > getInetAddresses( ) throws IPAddressException
{
ArrayList< InetAddress > arrayIPAddress = new ArrayList< InetAddress >( );
try
{
Enumeration< NetworkInterface > networkInterfaces = NetworkInterface.getNetworkInterfaces( );
if ( networkInterfaces == null )
{
throw new IPAddressException( "NetworkInterface Not found" );
}
while ( networkInterfaces.hasMoreElements( ) )
{
NetworkInterface card = ( NetworkInterface ) networkInterfaces.nextElement( );
Enumeration< InetAddress > addresses = card.getInetAddresses( );
if ( addresses == null )
{
continue;
}
while ( addresses.hasMoreElements( ) )
{
InetAddress inetAddress = ( InetAddress ) addresses.nextElement( );
arrayIPAddress.add( inetAddress );
}
}
}
catch ( SocketException obj )
{
throw new IPAddressException( "NetworkInterface Not found" );
}
return arrayIPAddress;
}
IPConfig报告:
C:\Documents and Settings\Administrator>ipconfig /all Windows IP Configuration Host Name . . . . . . . . . . . . : vm13autopassdl1 Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Unknown IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No DNS Suffix Search List. . . . . . : ind.hp.com india.hp.com Ethernet adapter Local Area Connection 2: Media State . . . . . . . . . . . : Media disconnected Description . . . . . . . . . . . : Intel(R) PRO/1000 MT Network Connection # 2 Physical Address. . . . . . . . . : 00-0C-29-90-80-41 Tunnel adapter Teredo Tunneling Pseudo-Interface: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface Physical Address. . . . . . . . . : FF-FF-FF-FF-FF-FF-FF-FF DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : fe80::ffff:ffff:fffd%4 Default Gateway . . . . . . . . . : NetBIOS over Tcpip. . . . . . . . : Disabled
有没有其他方法可以检测到机器不在网络中?
答案 0 :(得分:0)
我会尝试使用众所周知的端口(例如,80)连接到互联网上的知名网站(例如google.com,microsoft.com)。如果可以,那你就在互联网上;如果你不能,那么你可能不会。请注意,即使您在Internet上,企业防火墙规则仍可能会干扰您尝试执行的任何操作。
答案 1 :(得分:0)
除了环回地址127.0.0.1
和::1
之外,您还应该忽略:
fe8
,fe9
,fea
或feb
开头。224
到239
开始。ff
开头。