例如本机的IP地址是192.168.0.1
那么通过Java代码如何判断下面两个地址
127.0.0.1
192.168.0.1
是指向同一台机器呢java代码在本机运行

解决方案 »

  1.   

    或者判断一个InetAddress是否是本地IP也行
      

  2.   

    InetAddress.getAllByName(String host);不知道行不。
    --------------------------
    或者通过
    通过查找IP Address内容行不?例如下面有两个或者自己调用winapi 
    struct hostent * gethostbyname(const char *name);hostent 包含一个char **h_addr_list 就是所有的ip >ipconfig/allWindows IP Configuration        Host Name . . . . . . . . . . . . : chinese-c3e460f
            Primary Dns Suffix  . . . . . . . :
            Node Type . . . . . . . . . . . . : Mixed
            IP Routing Enabled. . . . . . . . : No
            WINS Proxy Enabled. . . . . . . . : No
            DNS Suffix Search List. . . . . . : mshome.netEthernet adapter 本地连接:        Connection-specific DNS Suffix  . : mshome.net
            Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethe
    rnet NIC
            Physical Address. . . . . . . . . : 00-40-46-01-62-8C
            Dhcp Enabled. . . . . . . . . . . : Yes
            Autoconfiguration Enabled . . . . : Yes
            IP Address. . . . . . . . . . . . : 192.168.0.57
            Subnet Mask . . . . . . . . . . . : 255.255.255.0
            Default Gateway . . . . . . . . . : 192.168.0.1
            DHCP Server . . . . . . . . . . . : 192.168.0.1
            DNS Servers . . . . . . . . . . . : 192.168.0.1
            Lease Obtained. . . . . . . . . . : 2005年12月12日 15:26:47
            Lease Expires . . . . . . . . . . : 2005年12月19日 15:26:47PPP adapter Vnet_PPPoE:        Connection-specific DNS Suffix  . :
            Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface
            Physical Address. . . . . . . . . : 00-53-45-00-00-00
            Dhcp Enabled. . . . . . . . . . . : No
            IP Address. . . . . . . . . . . . : 61.141.226.59
            Subnet Mask . . . . . . . . . . . : 255.255.255.255
            Default Gateway . . . . . . . . . : 61.141.226.59
            DNS Servers . . . . . . . . . . . : 202.96.134.133
                                                202.96.128.166
            NetBIOS over Tcpip. . . . . . . . : Disabled
      

  3.   

    InetAddress.getAllByName(String host);会返回一个数组InetAddress[]
      

  4.   

    InetAddress[] add1 = InetAddress.getAllByName("localhost")中会返回一个元素,是127.0.0.1
      

  5.   

    我不能为了这一点点小小的功能去调用win32Api,执行系统命令也不妥,况且linux下还是ifconfig,windows下是ipconfig
      

  6.   

    如果非127.0.0.1就可以InetAddress.getByName("192.168.0.1").getHostName();  --tinyjimmy
    InetAddress.getByName("192.168.2.1").getHostName();  --tinyjimmy而127.0.0.1被解析成localhost了对127.0.0.1特殊处理应该就可以了吧
      

  7.   

    public void getID() throws Exception {
    String s = "10.0.0.233";
    InetAddress[] ia = InetAddress.getAllByName("cncomp-dew3qujo");
    System.out.println("本机IP = " + InetAddress.getLocalHost());
    for (int i = 0; i < ia.length; i++) {
    if (s.equals(ia[i].getHostAddress()))
    System.out.println(ia[i].getHostAddress() + " 是本机IP");
    }
    }
      

  8.   

    但是在机器名未知的情况下如何读出所有的IP地址呢
    一个应用部署在多台机器,所以机器名是不固定的InetAddress.getAllByName("localhost");
    只能取到一个IP地址的
      

  9.   

    双网卡的IP也算,一个网卡设置了多个IP也算同一台机器SNMP就扯远了吧,还得依赖于系统的SNMP Agent
    还要第三方的SNMP开发包