日期:2014-05-20  浏览次数:20669 次

初学者 java网络编程的问题
//如下代码
Java code

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>ipconfig

PPP 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

------解决方案--------------------
你可以用锐捷客服端查看Ip的,你程序输入的是你连接到Internet的Ip,,,
------解决方案--------------------
PPP adapter是一个虚拟适配器,用来拨号连接,一般如果是拨号的话这个是你的外网地址
Ethernet adapter是你的物理网卡
------解决方案--------------------
Java code

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("---------------------");   
       
    }

}