Apache commons net 包介绍和简单使用详解编程语言

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 [email protected] SubnetUtils} class. 
 
 * @author Rory Winston <[email protected]> 
 
 * 
 
 */ 
 
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):

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10917.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论