集册 Java实例教程 将IPv4 INET地址转换为整数。

将IPv4 INET地址转换为整数。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

486
将IPv4 INET地址转换为整数。

/*

 * Copyright (c) 2004 by Cosylab

 *

 * The full license specifying the redistribution, modification, usage and other

 * rights and obligations is included with the distribution of this project in

 * the file "LICENSE-CAJ". If the license is not included visit Cosylab web site,

 * <http://www.cosylab.com>.

 *

 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE

 * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES

 * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,

 * OR REDISTRIBUTION OF THIS SOFTWARE.

 */

//package com.nowjava;

import java.net.Inet6Address;
/* 
 来自 
*N o w J a v a . c o m - 时  代  Java*/

import java.net.InetAddress;


public class Main {

    /** 

     * Convert an IPv4 INET address to an integer.

     * @param addr   IPv4 INET address.

     * @return integer representation of a given address.

     * @throws IllegalArgumentException if the address is really an IPv6 address

     */

    public static int ipv4AddressToInt(InetAddress addr) {


        if (addr instanceof Inet6Address)

            throw new IllegalArgumentException(

                    "IPv6 address used in IPv4 context");
                    /**
                     * n o w j a   v  a . c o m - 时  代  Java 提 供 
                  
展开阅读全文