Java examples for Network:IP Address
Compare two IP address byte array, the length must be 4 or 16.
//package com.java2s; public class Main { /**//from w w w.ja va 2s.c om * Compare two IP address byte array, the length must be 4 or 16. * @param address1 * @param address2 * @return */ private static boolean ifSameIpAddress(byte[] address1, byte[] address2) { if (address1 == null && address2 == null) return true; if (address1 != null && address2 != null && address1.length == address2.length) { //this IPv4 address if (address1.length == 4) { int a = getInteger(address1); int b = getInteger(address2); return a == b; } else { for (int i = 0; i < 16; i++) { if (address1[i] != address2[i]) return false; } return true; } } return false; } private static int getInteger(byte[] addr) { int address = 0; if (addr != null) { if (addr.length == 4) { address = addr[3] & 0xFF; address |= ((addr[2] << 8) & 0xFF00); address |= ((addr[1] << 16) & 0xFF0000); address |= ((addr[0] << 24) & 0xFF000000); } } return address; } }