+ /**
+ * Helper function that retrieves a local IPv4 address.
+ *
+ * @return string representation of an IPv4 address, or null if none found
+ */
+ private static String getLocalIPv4Address()
+ {
+ try
+ {
+ Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
+ while (en.hasMoreElements())
+ {
+ NetworkInterface intf = en.nextElement();
+
+ Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
+ while (enumIpAddr.hasMoreElements())
+ {
+ InetAddress inetAddress = enumIpAddr.nextElement();
+ if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4)
+ {
+ return inetAddress.getHostAddress().toString();
+ }
+ }
+ }
+ }
+ catch (SocketException ex)
+ {
+ ex.printStackTrace();
+ return null;
+ }
+ return null;
+ }
+