Java InetAddress Check isCommonSubnet(InetAddress address1, InetAddress address2)

Here you can find the source of isCommonSubnet(InetAddress address1, InetAddress address2)

Description

Checks if two IP addresses belong to the same subnet

License

Open Source License

Declaration

public static boolean isCommonSubnet(InetAddress address1, InetAddress address2) 

Method Source Code

//package com.java2s;
/**/*from  w w w .j a v  a2  s .  c om*/
* 
* Copyright (C) 2004-2008 FhG Fokus
*
* This file is part of the FhG Fokus UPnP stack - an open source UPnP implementation
* with some additional features
*
* You can redistribute the FhG Fokus UPnP stack and/or modify it
* under the terms of the GNU General Public License Version 3 as published by
* the Free Software Foundation.
*
* For a license to use the FhG Fokus UPnP stack software under conditions
* other than those described here, or to purchase support for this
* software, please contact Fraunhofer FOKUS by e-mail at the following
* addresses:
*   upnpstack@fokus.fraunhofer.de
*
* The FhG Fokus UPnP stack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

import java.net.InetAddress;

public class Main {
    /** Checks if two IP addresses belong to the same subnet */
    public static boolean isCommonSubnet(InetAddress address1, InetAddress address2) {
        // check source of request message
        byte[] address1data = address1.getAddress();
        byte[] address2data = address2.getAddress();

        // check if request is from local network
        return address1data[0] == address2data[0] && address1data[1] == address2data[1]
                && address1data[2] == address2data[2];
    }
}

Related

  1. isAddressReachable(InetAddress address)
  2. isAny(InetAddress ip)
  3. isBroadcastAddress(InetAddress address)
  4. IsBusy(final InetAddress remote, int port)
  5. isClassicAddress(InetAddress address)
  6. isGlobalAddressV6(InetAddress addr)
  7. isHostLocalHost(InetAddress host)
  8. isInet6Compatible(InetAddress address, Inet6Address inet6Address)
  9. isInRage(InetAddress check, InetAddress bcast, int netmask)