How to know programmatically whether a machne is in Global/Private network
I am trying to write a program in Java to know whether a machine is in Global/Private network or not. Below is my code snippet. I thought if a machine detects only loopback address(127.0.0.1
or ::1
) then it can be assumed that machine is not in network. But in one of the System where multicast is enabled, I am getting following IP Addresses other than loop back address.
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
Code snippet:
private static ArrayList< InetAddress > getInetAddresses( ) throws IPAddressException
{
ArrayList< InetAddress > arrayIPAddress = new ArrayList< InetAddress >( );
try
{
Enumeration< NetworkInterface > networkInterfaces = NetworkInterface.getNetworkInterfaces( );
if ( networkInterfaces == null )
{
throw new开发者_如何学JAVA 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 report:
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
Is there any other way to detect whethre machine is not in network?
I would try to see if you can connect to a well-known site on the Internet (e.g., google.com, microsoft.com) using a well-known port (e.g., 80). If you can, then you're on the Internet; if you can't, then you might as well not be. Note that corporate firewall rules could still interfere with whatever you're trying to do -- even if you're on the Internet.
In addition to the loopback addresses 127.0.0.1
and ::1
, you should also ignore:
- IPv6 Link-local addresses - these start with
fe8
,fe9
,fea
orfeb
. - IPv4 Multicast addresses - these start with
224
through to239
. - IPv6 Multicast addresses - these start with
ff
.
精彩评论