繁体   English   中英

如何以编程方式知道机器是否在全局/专用网络中

[英]How to know programmatically whether a machne is in Global/Private network

我正在尝试用Java编写程序,以了解一台计算机是否位于Global / Private网络中。 以下是我的代码段。 我认为如果一台机器仅检测到环回地址( 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

是否有其他方法可以检测Wethre机器不在网络中?

我将尝试查看您是否可以使用知名端口(例如80)连接到Internet上的知名站点(例如google.com,microsoft.com)。 如果可以,那么您就在互联网上; 如果不能的话,那你也最好不要这么做。 请注意,即使您在Internet上,公司防火墙规则仍可能会干扰您要尝试执行的操作。

除了回送地址127.0.0.1::1 ,您还应该忽略以下内容:

  • IPv6链路本地地址-这些以fe8fe9feafeb fe8
  • IPv4组播地址-这些地址从224239
  • IPv6组播地址-这些以ff开头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM