Apache commons net 包介绍和简单使用

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

Apache commons net 项目中封装了各种网络协议的客户端,支持的协议包括:

·        FTP

·        NNTP

·        SMTP

·        POP3

·        Telnet

·        TFTP

·        Finger

·        Whois

·        rexec/rcmd/rlogin

·        Time (rdate) andDaytime

·        Echo

·        Discard

·        NTP/SNTP

目前最新的版本为3.3.

和httpclient相比,net包更加侧重各种网络协议的使用,很多是专业网络方面的知识,对不了解的这些网络特性的情况,很难理解这些包的具体含义。

总而言之,这是一个强大的类,这是一个富有生命力,有专业精神的类。 :)

import java.util.Arrays;

import java.util.Scanner;

import org.apache.commons.net.util.SubnetUtils;

import org.apache.commons.net.util.SubnetUtils.SubnetInfo;

 

/**

 * Example class that shows how to use the {@link SubnetUtils} class.

 * @author Rory Winston <rwinston@apache.org>

 *

 */

public classSubnetUtilsExample {

 

   public static void main(String[] args) {

        String subnet = "192.168.38.1/31";

        SubnetUtils utils = new SubnetUtils(subnet);

        SubnetInfo info = utils.getInfo();

 

        System.out.printf("Subnet Information for %s:\n",subnet);

        System.out.println("--------------------------------------");

        System.out.printf("IP Address:\t\t\t%s\t[%s]\n",info.getAddress(),

                Integer.toBinaryString(info.asInteger(info.getAddress())));

        System.out.printf("Netmask:\t\t\t%s\t[%s]\n",info.getNetmask(),

                Integer.toBinaryString(info.asInteger(info.getNetmask())));

        System.out.printf("CIDR Representation:\t\t%s\n\n",info.getCidrSignature());

 

        System.out.printf("Supplied IP Address:\t\t%s\n\n",info.getAddress());

 

        System.out.printf("Network Address:\t\t%s\t[%s]\n",info.getNetworkAddress(),

                Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));

        System.out.printf("Broadcast Address:\t\t%s\t[%s]\n",info.getBroadcastAddress(),

                Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));

        System.out.printf("Low Address:\t\t\t%s\t[%s]\n",info.getLowAddress(),

                Integer.toBinaryString(info.asInteger(info.getLowAddress())));

        System.out.printf("High Address:\t\t\t%s\t[%s]\n",info.getHighAddress(),

                Integer.toBinaryString(info.asInteger(info.getHighAddress())));

 

        System.out.printf("Total usable addresses: \t%d\n",Integer.valueOf(info.getAddressCount()));

        System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses()));

 

        System.out.println("全部地址:" + Arrays.toString(info.getAllAddresses()));

        final String prompt ="Enter an IP address (e.g. 192.168.0.10):";

        System.out.println(prompt);

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()) {

            String address =scanner.nextLine();

           

            System.out.println("The IP address [" + address + "] is "

                    + (info.isInRange(address)? "": "not ")

                    + "within the subnet [" + subnet + "]");

            System.out.println(prompt);

        }

 

   }

 

}


运行结果如下:

Subnet Information for 192.168.38.1/31:

--------------------------------------

IP Address:         192.168.38.1    [11000000101010000010011000000001]

Netmask:            255.255.255.254 [11111111111111111111111111111110]

CIDR Representation:        192.168.38.1/31

 

Supplied IP Address:        192.168.38.1

 

Network Address:        192.168.38.0    [11000000101010000010011000000000]

Broadcast Address:      192.168.38.1    [11000000101010000010011000000001]

Low Address:            0.0.0.0 [0]

High Address:           0.0.0.0 [0]

Total usable addresses:     0

Address List: []

 

全部地址:[]

Enter an IP address (e.g.192.168.0.10):

标签: whois 网络

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:调节元素透明度的JS函数

下一篇: java实现RSA加密和解密