zoukankan      html  css  js  c++  java
  • Java InetAddress类的方法

    Java InetAddress类的方法

    这个类表示互联网协议(IP)地址。下面列出了 InetAddress 类常用的方法:

    序号方法描述
    1 static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。
    2 static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
    3 static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
    4 String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。
    5 String getHostName() 获取此 IP 地址的主机名。
    6 static InetAddress getLocalHost() 返回本地主机。
    7 String toString() 将此 IP 地址转换为 String。
    package com.joshua317;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Main {
        public static void main(String[] args){
            InetAddressTest inetAddress = new InetAddressTest();
            try {
                //根据域名获取
                InetAddress address1 = inetAddress.getInetAddress("www.baidu.com");
                System.out.println(address1);
                System.out.println(address1.getHostName() + "--" +address1.getHostAddress());
    
                //根据ip获取
                InetAddress address2 = inetAddress.getInetAddressByIp("103.235.46.39");
                System.out.println(address2);
                System.out.println(address2.getHostName() + "--" +address2.getHostAddress());
    
                //根据byte类型获取
                byte[] bytes = {103,34,46,39};
                InetAddress address3 = inetAddress.getInetAddress(bytes);
                System.out.println(address3);
    
                //如果数值超出Byte最大范围,需要进行转换,例如:(byte)(235 &0xff)
                System.out.println("Byte最小值:" + Byte.MIN_VALUE);
                System.out.println("Byte最大值:" + Byte.MAX_VALUE);
                byte[] bytes2 = {103,(byte)(235 &0xff),46,39};
                InetAddress address4 = inetAddress.getInetAddress(bytes2);
                System.out.println(address4);
    
                //查看是否是回环地址
                InetAddress address5 = inetAddress.getInetAddressByIp("127.0.0.1");
                System.out.println(address5.isLoopbackAddress());
                //查看是否是本地地址
                System.out.println(address5.isLinkLocalAddress());
    
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }
    @SuppressWarnings("all")
    class InetAddressTest {
    
        public InetAddressTest() {
        }
    
        public InetAddress getInetAddress(String host) throws UnknownHostException{
            return InetAddress.getByName(host);
        }
        public InetAddress getInetAddress(byte[] host) throws UnknownHostException{
            return InetAddress.getByAddress(host);
        }
    
        public InetAddress getInetAddress(String host,byte[] ipByte) throws UnknownHostException{
            return InetAddress.getByAddress(host, ipByte);
        }
        public InetAddress getInetAddressByIp(String ip) throws UnknownHostException{
            String[] ipStr = ip.split("\\.");
            byte[] ipByte = new byte[4];
            for (int i =0; i<ipByte.length; i++) {
                ipByte[i] = (byte) (Integer.parseInt(ipStr[i]) &0xff);
            }
            return InetAddress.getByAddress(ipByte);
        }
    }
    

     

  • 相关阅读:
    ABAP 程序中的类 沧海
    ABAP类的方法(转载) 沧海
    More than 100 ABAP Interview Faq's(2) 沧海
    SAP and ABAP Memory总结 沧海
    ABAP Frequently Asked Question 沧海
    ABAP System Reports(Additional functions) 沧海
    ABAP Questions Commonly Asked 1 沧海
    ABAP Tips and Tricks 沧海
    ABAP System Fields 沧海
    ABAP 面试问题及答案(一):数据库更新及更改 SAP Standard (转) 沧海
  • 原文地址:https://www.cnblogs.com/joshua317/p/15720638.html
Copyright © 2011-2022 走看看