创建 InetAddress 对象的四个静态方法

package InetAddressClass;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressStaticMethod {

    
    public static void main(String[] args) {
        
        InetAddressStaticMethod._getLocalHost();
        InetAddressStaticMethod._getByName("www.csdn.net");
        InetAddressStaticMethod._getAllByName("www.baidu.com");
        InetAddressStaticMethod._getByAddress();
    }
    
    
    public static void _getLocalHost() {
        
        InetAddress localAddress = null;
        try {
            localAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(localAddress);
        System.out.println(localAddress.toString().split("/")[1]);
    }
    
    
    public static void _getByName(String hostname) {
        
        InetAddress address = null;
        try {
            address = InetAddress.getByName(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(address);
        System.out.println(address.toString().split("/")[1]);        
    }
    
    
    
    public static void _getAllByName(String hostname) {
        
        InetAddress[] addresses = null;
        try {
            addresses = InetAddress.getAllByName(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        for(InetAddress address : addresses) {
            System.out.println(address);
            System.out.println(address.toString().split("/")[1]);            
        }
    }

    
    public static void _getByAddress() {
        
        byte[] ip = new byte[] { (byte)141, (byte)146, 8, 66 };
        
        InetAddress address1 = null;
        try {
            address1 = InetAddress.getByAddress(ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(address1);
        
        InetAddress address2 = null;
        try {
            address2 = InetAddress.getByAddress("Oracle website", ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(address2);
    }
    
    
    

    
//    java.net.InetAddress.java -- source code:

//    public String toString() {
//        String hostName = holder().getHostName();
//        return ((hostName != null) ? hostName : "")
//            + "/" + getHostAddress();
//    }
    
    
//    public static InetAddress getByName(String host)
//            throws UnknownHostException {
//            return InetAddress.getAllByName(host)[0];
//        }    
    
    
}

 

转载于:https://www.cnblogs.com/starzou/p/4112115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值