如果有多块网卡,如何获得某特定网卡的IP地址?最不希望得到的结果是127.0.0.1 呵呵……怎样才能得到有意义的IP地址呢?也就是连接入Internet或者局域网中的时候被别人所识别的IP地址。ServerSocket server = new ServerSocket(port);
System.out.println(server.getLocalAddress());这样得到的竟然是0.0.0.0

解决方案 »

  1.   

    InetAddress address = InetAddress.getLocalHost();
    System.out.println(address.getHostAddress());用上面的语句另外再运行的时候,你可能,还需要加入一个用-D 指定的策略文件java xxx -Djava.security.policy=xxxxxx
      

  2.   

    try{
    InetAddress a = InetAddress.getLocalHost();
    System.out.println (a);
    }catch(Exception e){
    }
      

  3.   

    InetAddress ip= InetAddress.getLocalHost();
      

  4.   

    得到的是
    192.168.0.88嗬嗬,函数起作用了,不过我想知道的是我上Internet的IP地址,例如218.25.147.154
    如何才能得到呢?目前是通过PPPoE协议使用ADSL上网。另外,如果是安装了2块以上网卡也会出现多个IP地址的问题,如何找到自己需要的呢?
      

  5.   

    try{
    InetAddress localHostAddress =InetAddress.getLocalHost();
    System.out.println(localHostAddress);
    }
    catch(Exception e){}
      

  6.   

    import java.net.*;public class MyAddress
      {
         public static void main(String[] args)
           {
             try
              {
                InetAddress address=InetAddress.getLocalHost();
                System.out.println(address);
              }
            catch(UnknownHostException e)
              {
                System.out.println("Could not find this computer's address.");
               }
            }
        }
      

  7.   

    不好意思!应该为:
    try
         {
           InetAddress[] addresses=InetAddress.getLocalhost();
           for(int i=0;i<addresses.length;i++)
             {
               System.out.println(addresses[i]);
              }
         }
           catch(UnknownHostException e)
              {
                System.out.println("Could not find www.microsoft.com");
               }
      

  8.   

    import java.net.*;
    public class NetTool{
         InetAddress myIPaddress=null;
         InetAddress myServer=null;
         
         public static void main(String[] args){
          NetTool mytool;
          mytool=new NetTool();
         
          System.out.println("Your host IP is:"+mytool.getMyIP());
          System.out.println("The Server IP is:"+mytool.getServerIP());
         }
         
         public InetAddress getMyIP(){
          try{
              myIPaddress=InetAddress.getLocalHost();
          }catch(UnknownHostException e){}
          return (myIPaddress);
         }
         public InetAddress getServerIP(){
          try{
              myServer=InetAddress.getByName("www.abc.com");
          }catch(UnknownHostException e){}
          return (myServer);
         }
         }
      

  9.   

    King_Play的问题我也有同感. 用InetAdress.getLocalHost() 确实不能得到我们的真实IP. 我也是用通过PPPoE协议使用ADSL上网. 怎么办呢? KingPlay你现在找到解决的方法吗? 各位前辈,难道没办法吗?
      

  10.   

    linux下 InetAddress.getLocalHost()得到 127.0.0.1 ,jdk1.4.1 , 如何获得本机地址呢?