码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-11-21 10:29:08      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   for   

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];
//        }    
    
    
}

 

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

标签:style   blog   io   ar   color   os   sp   java   for   

原文地址:http://www.cnblogs.com/starzou/p/4112115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!