try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { hostIP=ia.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); }