Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns the current local IP address or an empty string in error case /
- * when no network connection is up.
- * <p>
- * The current machine could have more than one local IP address so might
- * prefer to use {@link #getAllLocalIPs() } or
- * {@link #getAllLocalIPs(java.lang.String) }.
- * <p>
- * If you want just one IP, this is the right method and it tries to find
- * out the most accurate (primary) IP address. It prefers addresses that
- * have a meaningful dns name set for example.
- *
- * @return Returns the current local IP address or an empty string in error case.
- * @since 0.1.0
- */
- public static String getLocalIP()
- {
- //// This method does not work any more - I think on Windows it worked by accident
- //try
- //{
- // String ip = InetAddress.getLocalHost().getHostAddress();
- // return ip;
- //}
- //catch (UnknownHostException ex)
- //{
- // Logger.getLogger(Net.class.getName()).log(Level.WARNING, null, ex);
- // return "";
- //}
- String ipOnly = "";
- try
- {
- Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
- if (nifs == null) return "";
- while (nifs.hasMoreElements())
- {
- NetworkInterface nif = nifs.nextElement();
- // We ignore subinterfaces - as not yet needed.
- if (!nif.isLoopback() && nif.isUp() && !nif.isVirtual())
- {
- Enumeration<InetAddress> adrs = nif.getInetAddresses();
- while (adrs.hasMoreElements())
- {
- InetAddress adr = adrs.nextElement();
- if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()))
- {
- String adrIP = adr.getHostAddress();
- String adrName;
- if (nif.isPointToPoint()) // Performance issues getting hostname for mobile internet sticks
- adrName = adrIP;
- else
- adrName = adr.getCanonicalHostName();
- if (!adrName.equals(adrIP))
- return adrIP;
- else
- ipOnly = adrIP;
- }
- }
- }
- }
- if (ipOnly.length()==0) Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available");
- return ipOnly;
- }
- catch (SocketException ex)
- {
- Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available", ex);
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement