Demo entry 6659270

ip

   

Submitted by Leckie on Nov 10, 2017 at 09:41
Language: Java. Code size: 1.2 kB.

public static boolean localIpContains(String validIp) {
		try {
			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
            	NetworkInterface interf = interfaces.nextElement();
                if (interf.isLoopback() || !interf.isUp() || interf.isVirtual()) {
                    // filter the loopback interface
                    // filter the not running interface
                    // filter the virtual interface
                    continue;
                }
                Enumeration<InetAddress> inetAddresses = interf.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (inetAddress instanceof Inet4Address) {
                    	if(validIp.equalsIgnoreCase(inetAddress.getHostAddress())) {
                    		return true;
                      	}
                    }
                }
            }
        } catch (SocketException e1) {
                e1.printStackTrace();
        }
		return false;
	}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).