//如下代码package laobao;
import java.net.InetAddress;
public class Demo01 {
public static void main(String args[]) throws Exception{
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println("本地IP :" + localAddress.getHostAddress() );
}
}//结果 本地IP :111.0.229.245在cmd中 :
C:\Users\laobao>ipconfigPPP adapter VPN 连接:   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 111.0.229.245
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . : 0.0.0.0
 
   ......Ethernet adapter 本地连接:   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::2975:65eb:1793:ac3f%10
   IPv4 Address. . . . . . . . . . . : 10.10.221.xx
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.10.221.1  ......请问这里的  PPP adapter VPN 连接   和   Ethernet adapter 本地连接  各是什么?
结果为什么不是  本地IP :10.10.221.xx  ?本人用的是学校的 锐捷  本地连接手动输入的ip 10.10.221.xx

解决方案 »

  1.   

    你可以用锐捷客服端查看Ip的,你程序输入的是你连接到Internet的Ip,,,
      

  2.   

    PPP adapter是一个虚拟适配器,用来拨号连接,一般如果是拨号的话这个是你的外网地址
    Ethernet adapter是你的物理网卡
      

  3.   


    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;
    public class GetIpsInfo {
        public static void main(String[] args) {
            Enumeration nettworkInterfaces = null;
            try {
                nettworkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (nettworkInterfaces.hasMoreElements()) {
             System.out.println("---------------------------------------------------------");
                    NetworkInterface networkInterface = (NetworkInterface) nettworkInterfaces.nextElement();
                    System.out.println("DisplayName = " + networkInterface.getDisplayName());
                    System.out.println("       Name = " + networkInterface.getName());
                    Enumeration inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        System.out.println("这块网卡的IP = "
                                + ((InetAddress) inetAddresses.nextElement()).getHostAddress());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    System.out.println("---------------------------------------------------------");   
           
        }}
      

  4.   

    localAddress.getHostAddress() 这样获得的就是ipconfig得到的ip
      

  5.   

    localAddress.getHostAddress() 这样获得的就是ipconfig得到的ip
      

  6.   

    PPP adapter是一个虚拟适配器,用来拨号连接,一般如果是拨号的话这个是你的外网地址
    Ethernet adapter是你的物理网卡
      

  7.   

    不是的,getHostAddress是根据hostname获取的ip如果在host文件里配置一下就会发现ip不对了