Android get local IP address, Ipv4 address check, Ipv6 address check

Source: Internet
Author: User

/**
* Get local IP address
* @author YOLANDA
* @return
*/
Public static String getLocalIPAddress() {
String ipAddress = "";
Try {
Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces();
/ / Traverse the network interface used
While (netfaces.hasMoreElements()) {
NetworkInterface nif = netfaces.nextElement();// Get the address bound to each network interface
Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
// traverse all ips bound to each interface
While (inetAddresses.hasMoreElements()) {
InetAddress ip = inetAddresses.nextElement();
If (!ip.isLoopbackAddress() && isIPv4Address(ip.getHostAddress())) {
ipAddress = ip.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
Return ipAddress;
}

/* here to add a little explanation, because at the beginning of 21,httpClient was abandoned,Google recommended the use of Urlconnect, where the Ipv4 is also abandoned,
in order to be compatible with later versions,I httpclient some of the source code directly to the project to use,so here appears the Ipv4 check the source */

/**
* Ipv4 address check
*/
Private static final Pattern IPV4_PATTERN = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0- 9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4 ][0-9]|25[0-5])$");
/**
* Check if it is a valid IPV4 address
* @param input the address string to check for validity
* @return true if the input parameter is a valid IPv4 address
*/
Public static boolean isIPv4Address(final String input) {
Return IPV4_PATTERN.matcher(input).matches();
}

/*=========== The following is an IPv6 check, temporarily not used ==========*/

/*//Uncompressed IPv6 address check
Private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");

/ / Check the standard (uncompressed) IPv6 address of the parameter is valid
Public static boolean isIPv6StdAddress(final String input) {
Return IPV6_STD_PATTERN.matcher(input).matches();
}


//Compressed IPv6 address check
Private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile(
"^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" + // 0-6 Hex fields
"::" +
"(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0- 6 hex fields
/ / Check whether the parameter is valid to compress the IPv6 address
Public static boolean isIPv6HexCompressedAddress(final String input) {
Int colonCount = 0;
For(int i = 0; i < input.length(); i++) {
If (input.charAt(i) == ':') {
colonCount++;
}
}
Return colonCount <= 7 && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
}

/ / Check if the compressed or uncompressed IPV6 address
Public static boolean isIPv6Address(final String input) {
Return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);
}*/ 

Android get local IP address, Ipv4 address check, Ipv6 address check

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.