SHARE
TWEET

Untitled

a guest Jul 15th, 2010 1,114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * Returns the current local IP address or an empty string in error case /
  3.      * when no network connection is up.
  4.      * <p>
  5.      * The current machine could have more than one local IP address so might
  6.      * prefer to use {@link #getAllLocalIPs() } or
  7.      * {@link #getAllLocalIPs(java.lang.String) }.
  8.      * <p>
  9.      * If you want just one IP, this is the right method and it tries to find
  10.      * out the most accurate (primary) IP address. It prefers addresses that
  11.      * have a meaningful dns name set for example.
  12.      *
  13.      * @return  Returns the current local IP address or an empty string in error case.
  14.      * @since   0.1.0
  15.      */
  16.     public static String getLocalIP()
  17.     {
  18.  
  19.         //// This method does not work any more - I think on Windows it worked by accident
  20.         //try
  21.         //{
  22.         //    String ip = InetAddress.getLocalHost().getHostAddress();
  23.         //    return ip;
  24.         //}
  25.         //catch (UnknownHostException ex)
  26.         //{
  27.         //    Logger.getLogger(Net.class.getName()).log(Level.WARNING, null, ex);
  28.         //    return "";
  29.         //}
  30.         String ipOnly = "";
  31.         try
  32.         {
  33.             Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
  34.             if (nifs == null) return "";
  35.             while (nifs.hasMoreElements())
  36.             {
  37.                 NetworkInterface nif = nifs.nextElement();
  38.                 // We ignore subinterfaces - as not yet needed.
  39.  
  40.                 if (!nif.isLoopback() && nif.isUp() && !nif.isVirtual())
  41.                 {
  42.                     Enumeration<InetAddress> adrs = nif.getInetAddresses();
  43.                     while (adrs.hasMoreElements())
  44.                     {
  45.                         InetAddress adr = adrs.nextElement();
  46.                         if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()))
  47.                         {
  48.                             String adrIP = adr.getHostAddress();
  49.                             String adrName;
  50.                             if (nif.isPointToPoint()) // Performance issues getting hostname for mobile internet sticks
  51.                                 adrName = adrIP;
  52.                             else
  53.                                 adrName = adr.getCanonicalHostName();
  54.  
  55.                             if (!adrName.equals(adrIP))
  56.                                 return adrIP;
  57.                             else
  58.                                 ipOnly = adrIP;
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.             if (ipOnly.length()==0) Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available");
  64.             return ipOnly;
  65.         }
  66.         catch (SocketException ex)
  67.         {
  68.             Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available", ex);
  69.             return "";
  70.         }
  71.     }
RAW Paste Data
Top