tudzic

Untitled

Oct 30th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public static String getIpAddress() {
  2.         try {
  3.             for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en
  4.                     .hasMoreElements();) {
  5.                 NetworkInterface intf = (NetworkInterface) en.nextElement();
  6.                 for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr
  7.                         .hasMoreElements();) {
  8.                     InetAddress inetAddress = (InetAddress) enumIpAddr
  9.                             .nextElement();
  10.                     if (!inetAddress.isLoopbackAddress()
  11.                             && inetAddress instanceof Inet4Address) {
  12.                         String ipAddress = inetAddress.getHostAddress()
  13.                                 .toString();
  14.                         System.out.println("IP address: " + ipAddress);
  15.                         return ipAddress;
  16.                     }
  17.                 }
  18.             }
  19.         } catch (Exception ex) {
  20.             System.out
  21.             .println("Socket exception in GetIP Address of Utilities");
  22.         }
  23.         return null;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment