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);
        }
    }
    

     

  • 相关阅读:
    配置secureCRT
    LINUX的网口绑定(bond)
    背包形动态规划 fjutoj2375 金明的预算方案
    背包形动态规划 fjutoj1380 Piggy-Bank
    背包形动态规划 fjutoj2347 采药
    树形动态规划 fjutoj-2392 聚会的快乐
    树形动态规划 fjutoj-2131 第四集,聚集城市
    andriod开发--使用Http的Get和Post方式与网络交互通信
    线段树复合标记
    图论之拓扑排序 poj 2367 Genealogical tree
  • 原文地址:https://www.cnblogs.com/joshua317/p/15720638.html
Copyright © 2011-2022 走看看