String ipaddr = null;
try {
boolean isLoopBack = true;
Enumeration<NetworkInterface> en;
en = NetworkInterface.getNetworkInterfaces();
while(en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
if (ni.isLoopback())
continue;
Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
while(inetAddresses.hasMoreElements()) {
InetAddress ia = inetAddresses.nextElement();
if (ia.getHostAddress() != null && ia.getHostAddress().indexOf(".") != -1) {
ipaddr = ia.getHostAddress();
System.out.println(ipaddr);
isLoopBack = false;
break;
}
}
if (!isLoopBack)
break;
}
System.out.println(" IP = " + ipaddr);
} catch (SocketException e) {
e.printStackTrace();
}